人工智能实践 | VGG-16迁移模型( 二 )


bgr=tf.concat(axis=3,values=[
blue-self.vgg_mean[0],
green-self.vgg_mean[1],
red-self.vgg_mean[2],
])
#pre-trainedVGGlayersarefixedinfine-tune
conv1_1=self.conv_layer(bgr,"conv1_1")
conv1_2=self.conv_layer(conv1_1,"conv1_2")
pool1=self.max_pool(conv1_2,'pool1')
conv2_1=self.conv_layer(pool1,"conv2_1")
conv2_2=self.conv_layer(conv2_1,"conv2_2")
pool2=self.max_pool(conv2_2,'pool2')
conv3_1=self.conv_layer(pool2,"conv3_1")
conv3_2=self.conv_layer(conv3_1,"conv3_2")
conv3_3=self.conv_layer(conv3_2,"conv3_3")
pool3=self.max_pool(conv3_3,'pool3')
conv4_1=self.conv_layer(pool3,"conv4_1")
conv4_2=self.conv_layer(conv4_1,"conv4_2")
conv4_3=self.conv_layer(conv4_2,"conv4_3")
pool4=self.max_pool(conv4_3,'pool4')
conv5_1=self.conv_layer(pool4,"conv5_1")
conv5_2=self.conv_layer(conv5_1,"conv5_2")
conv5_3=self.conv_layer(conv5_2,"conv5_3")
pool5=self.max_pool(conv5_3,'pool5')
#detachoriginalVGGfclayersand
#reconstructyourownfclayersserveforyourownpurpose
self.flatten=tf.reshape(pool5,[-1,7*7*512])
self.fc6=tf.layers.dense(self.flatten,256,tf.nn.relu,name='fc6')
self.out=tf.layers.dense(self.fc6,1,name='out')
self.sess=tf.Session
ifrestore_from:
saver=tf.train.Saver
saver.restore(self.sess,restore_from)
else:#traininggraph
self.loss=tf.losses.mean_squared_error(labels=self.tfy,predictions=self.out)
self.train_op=tf.train.RMSPropOptimizer(0.001).minimize(self.loss)
self.sess.run(tf.global_variables_initializer)
defmax_pool(self,bottom,name):
returntf.nn.max_pool(bottom,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name=name)
defconv_layer(self,bottom,name):
withtf.variable_scope(name):#CNN'sfilterisconstant,NOTVariablethatcanbetrained
conv=tf.nn.conv2d(bottom,self.data_dict[name][0],[1,1,1,1],padding='SAME')
lout=tf.nn.relu(tf.nn.bias_add(conv,self.data_dict[name][1]))
人工智能实践 | VGG-16迁移模型】returnlout
deftrain(self,x,y):
loss,_=self.sess.run([self.loss,self.train_op],{self.tfx:x,self.tfy:y})
returnloss
defpredict(self,paths):
fig,axs=plt.subplots(1,2)
fori,pathinenumerate(paths):
x=load_img(path)
length=self.sess.run(self.out,{self.tfx:x})
axs[i].imshow(x[0])
axs[i].set_title('Len:%.1fcm'%length)
axs[i].set_xticks();
axs[i].set_yticks()
plt.show
defsave(self,path='./for_transfer_learning/model/transfer_learn'):
saver=tf.train.Saver
saver.save(self.sess,path,write_meta_graph=False)
deftrain:
tigers_x,cats_x,tigers_y,cats_y=load_data
#plotfakelengthdistribution
plt.hist(tigers_y,bins=20,label='Tigers')
plt.hist(cats_y,bins=10,label='Cats')
plt.legend
plt.xlabel('length')
plt.show
xs=np.concatenate(tigers_x+cats_x,axis=0)
ys=np.concatenate((tigers_y,cats_y),axis=0)
vgg=Vgg16(vgg16_npy_path='./for_transfer_learning/vgg16.npy')
print('Netbuilt')
foriinrange(100):
b_idx=np.random.randint(0,len(xs),6)
train_loss=vgg.train(xs[b_idx],ys[b_idx])
print(i,'trainloss:',train_loss)
vgg.save('./for_transfer_learning/model/transfer_learn')#savelearnedfclayers
defeval:
vgg=Vgg16(vgg16_npy_path='./for_transfer_learning/vgg16.npy',
restore_from='./for_transfer_learning/model/transfer_learn')
vgg.predict(
['./for_transfer_learning/data/kittycat/23066047.d6694f.jpg','./for_transfer_learning/data/tiger/37425296_58a9896259.jpg'])
if__name__=='__main__':
#download
#train
eval
实例讲解
人工智能
精彩回顾
构建领域知识图谱
A*算法实现最优路径规划
基于规则产生式的推理
4
参考书籍
《人工智能》
尚文倩编著
定价:59.8元