邀请共同入坑好玩的东西:MEMNET评价照片好坏的开源caffe代码

网址:http://memorability.csail.mit.edu/demo.html

运行的结果好像和DEMO差别很大,正在苦调试

给位有人共同尝试一下这个好玩的代码么?
已邀请:

xiesiyuan - 隐居者

赞同来自: caffe alex68

1.下载:
利用wget 或其他命令从 http://memorability.csail.mit.edu/download.html   下载  memnet.tar.gz 到caffe根目录下,并用tar 解压缩之。

2. 调试
我的系统是ubuntu 16.04,GTX940,caffe cuDnn v4.
再caffe/python目录下打开gedit,保存如下代码的文件,并命名为memnet.py
注意,
1)MODEL_FILE
DEPLOY_FILE
MEAN_FILE
要根据你电脑上的caffe路径来做修改,我是存储在~/caffe/memnet下的
2)test1.jpg -test8.jpg是我从Demo中扣下来做测试的8张图片,你可以选择为任意图片,但要把caffe.io.Transformer后面的地址更新一下。
3)备注:前提,你的机器要是安装过基于cuDnn的caffe
#!/usr/bin/python

import caffe
import numpy as np

MODEL_FILE="/home/linuxhost/caffe/memnet/memnet.caffemodel"
DEPLOY_FILE="/home/linuxhost/caffe/memnet/deploy.prototxt"
MEAN_FILE="/home/linuxhost/caffe/memnet/mean.npy"
net=caffe.Net(DEPLOY_FILE,MODEL_FILE,caffe.TEST)

imagemean=np.load(MEAN_FILE)
imagemean=imagemean.mean(1).mean(1)
print('mean-subtracted values:',zip('BGR',imagemean))

# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
transformer.set_mean('data', imagemean) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR


# set the size of the input (we can skip this if we're happy
# with the default; we can also change it later, e.g., for different batch sizes)
#net.blobs['data'].reshape(50, # batch size
# 3, # 3-channel (BGR) images
# 227, 227) # image size is 227x227

caffe.set_device(0)
caffe.set_mode_gpu()

input_image1=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test1.jpg")

transformed_image = transformer.preprocess('data', input_image1)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image2=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test2.jpg")

transformed_image = transformer.preprocess('data', input_image2)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image3=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test3.jpg")

transformed_image = transformer.preprocess('data', input_image3)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image4=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test4.jpg")

transformed_image = transformer.preprocess('data', input_image4)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image5=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test5.jpg")

transformed_image = transformer.preprocess('data', input_image5)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image6=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test6.jpg")

transformed_image = transformer.preprocess('data', input_image6)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image7=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test7.jpg")

transformed_image = transformer.preprocess('data', input_image7)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)

input_image8=caffe.io.load_image("/home/linuxhost/caffe/memnet/test/test8.jpg")

transformed_image = transformer.preprocess('data', input_image8)
net.blobs['data'].data[...] = transformed_image

output=net.forward()

print(output)
3. 开始运行
cd ~/caffe/python
python memnet.py

输出是一列数字都一样的向量,其中的向量值就是检测这张图片有多么吸引人的打分。

Screenshot_from_2016-09-12_00-15-00.jpg


4 附注
代码中有一行
imagemean=imagemean.mean(1).mean(1)
是因为如果不这样做,会报错:
'Mean shape incompatible with input shape.'

另一种更合理的做法其实是把caffe/python/caffe/io.py文件中的
 if ms != self.inputs[in_][1:]:
raise ValueError('Mean shape incompatible with input shape.')
改为
 if ms != self.inputs[in_][1:]:
print(self.inputs[in_])
in_shape = self.inputs[in_][1:]
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
mean = resize_image(normal_mean.transpose((1,2,0)),
in_shape[1:]).transpose((2,0,1)) * \
(m_max - m_min) + m_min
# raise ValueError('Mean shape incompatible with input shape.')

并把memnet.py中的
imagemean=imagemean.mean(1).mean(1)
改为
#imagemean=imagemean.mean(1).mean(1)
因为io.py的模块好像在这个部分是有Bug的。

爱在路上 - 想成为菜鸟中的战斗机

赞同来自: Artisgrammer

一入此坑深似海,从此妹纸是路人

xiesiyuan - 隐居者

赞同来自:

终于跑通了。原来是caffe python的io.py模块有问题。
 
给图片构图评分的感觉还是很爽的。后续贴上过程和代码。

心受天竺

赞同来自:

你好,我看lamen.tar.gz里的label都是浮点型的,但是训练用的数据是lmdb类型的。我看到有些地方说lmdb不支持浮点类型啊,请问他是怎么实现的啊

anan1205

赞同来自:

HI,您好,这个是评价图像的质量的吗?

要回复问题请先登录注册