PixelNet
PixelNet
二胡艺 发表于11个月前
PixelNet
  • 发表于 11个月前
  • 阅读 13
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

(一)基于googlenet的model

%matplotlib inline
import numpy as np
import cv2
import caffe
import matplotlib.pyplot as plt

model='deploy.prototxt'
weight = 'bvlc_googlenet.caffemodel'
filename='../images/2007_002619.jpg'
caffe.set_mode_gpu()
net = caffe.Net(model,weight,caffe.TEST)
transformer = caffe.io.Transformer({'data':(10,3,224,224)})
transformer.set_transpose('data',(2,0,1))
#transformer.set_mean('data',np.load(meanfile).mean(1).mean(1))
transformer.set_raw_scale('data',255)
transformer.set_channel_swap('data',(2,1,0))

img = caffe.io.load_image(filename)
net.blobs['data'].data[...] = transformer.preprocess('data',img)
net.forward()
blob = net.blobs['prob'].data[0]
idx = blob.argmax()
plt.imshow(img)
print idx,blob[idx]

#for layer_key,layer_blob in net.blobs.iteritems():
#    print layer_key,layer_blob.data.shape
584 0.257885

输入图片说明

(二)可视化feature

def vis(data):
    _,C,H,W = data.shape
    d = data[0]
    n = int(np.sqrt(C))
    k = 1
    plt.figure(figsize=(64,64))
    for c in range(C):
        plt.subplot(n,n+1,k)
        k += 1
        img = d[c,:,:]
        plt.imshow(img,cmap='jet');plt.axis('off')
    plt.show()

def vis_k(data):
    print data.shape
    N,C,H,W = data.shape
    d = data[0]
    nc = 16
    k = 1
    if(N > 64):
        N = 10
    nr = int(N * C / nc)
    plt.figure(figsize=(64,64))
    for n in range(N):
        for c in range(C):
            img = data[n,c,:,:]
            plt.subplot(nr+1,nc,k)
            k += 1
            plt.imshow(img,cmap='jet');plt.axis('off')
    plt.show()
    
data = net.blobs['conv1/7x7_s2'].data
vis(data)
kernel = net.params['conv2/3x3'][0].data
#print kernel.shape
#vis_k(kernel)

输入图片说明

(三)提取feature map 的hyperColumn,然后用kmean进行聚类

from sklearn.cluster import KMeans

def upsample(data,size=(224,224)):    
    C,H,W = data.shape
    data_ = np.zeros((C,size[0],size[1]))
    for c in range(C):
        im = data[c,:,:]
        im = cv2.resize(im,size)
        data_[c,:,:] = im
    return data_
def hyperColumn(data):
    C,H,W = data.shape
    data_ = np.zeros((H * W,C))
    for h in range(H):
        for w in range(W):
            d = data[:,h,w]
            data_[h*W+w,:]=d
    return data_

conv1 = net.blobs['conv1/7x7_s2'].data[0]
conv2 = net.blobs['conv2/3x3'].data[0]
conv3 = net.blobs['inception_3a/1x1'].data[0]
conv1_ = upsample(conv1,size=(224,224))
conv2_ = upsample(conv2,size=(224,224))
conv3_ = upsample(conv3,size=(224,224))
data = np.concatenate((conv1_,conv2_,conv3_),axis=0)
trainData = hyperColumn(data)
clf = KMeans(n_clusters=5,max_iter=5000,n_init=5,n_jobs=-1)
clf.fit(trainData)
label = clf.labels_
result = label.reshape(224,224)
plt.imshow(result)

输入图片说明

标签: caffe pycaffe PixelNet
共有 人打赏支持
粉丝 3
博文 42
码字总数 12555
×
二胡艺
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: