【游戏开发】Tentillflow 图片爬虫和随机森林算法省份代码表单样式
说到迁移学习,讲的通俗点就是,利用别人的已经训练过的,只要训练结尾,就可以快速训练模型,就是站在巨人的肩膀上做事的道理!话不多说,直接看代码!在这里重重感谢莫烦老师!注:我的开发环境是VS2017!
这个例子是区分猫和老虎的例子!首先,我们需要准备大量猫和老虎的照片,这里可以百度图片里爬虫得到!
def Search(name,localpath,page):
'tn' : 'resultjsonavatarnew',
'ie' : 'utf-8',
'cg' : '',
'itg' : '',
'z' : '0',
'fr' : '',
'width' : '',
'height' : '',
'lm' : '-1',
'ic' : '0',
's' : '0',
'word' : name,
'st' : '-1',
'gsm' : '',
'rn' : '30'
};
Request(params,localpath)
return ;
response = requests.get(searchurl,params =param )
filename = os.path.split(json[i]['objURL'])[1]
Download(json[i]['objURL'],filename,path)
try:
urlretrieve(url,path)
print('Downloading Images From ', url)
except:
print('Downloading None Images!')
Search('老虎','data/tiger',i)
Search('猫','data/cat',i)
下面步入我们的重点 -----迁移学习:我用的是 vgg16 别人训练好的一半模型,在加上一部分自己的,就构成了自己的一套模型!
我们用到的vgg16.npy --百度云:点击打开链接
我们逐行解释from urllib.request import urlretrieve
import os
import numpy as np
import tensorflow as tf
import skimage.io
import skimage.transform
import matplotlib.pyplot as plt
#这段代码是将文件夹中的图片读出来,并且统一好规格,224*224的规格
def load_img(path):
yy = int((img.shape[0] - short_edge) / 2)
xx = int((img.shape[1] - short_edge) / 2)
crop_img = img[yy: yy + short_edge, xx: xx + short_edge]
continue
try:
resized_img = load_img(os.path.join(dir, file))
except OSError:
continue
imgs[k].append(resized_img)
if len(imgs[k]) == 400:
break
print('***',k, len(imgs[k]))
self.data_dict = np.load(vgg16_npy_path, encoding='latin1').item() #这里我们将vgg16.npy里的参数导入
print('Something error about the vgg_npy')
self.tfy = tf.placeholder(tf.float32, [None, 1])
#这里需要将图片的rgb格式转化为bgr格式。
green - self.vgg_mean[1],
red - self.vgg_mean[2],
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')
self.fc_6 = tf.layers.dense(self.flatten,256,tf.nn.relu,name = 'fc6')
self.out = tf.layers.dense(self.fc_6,1,name = 'fc_out')
if restore_from:
saver.restore(self.sess, restore_from)
self.train_op = tf.train.RMSPropOptimizer(0.001).minimize(self.loss)
self.sess.run(tf.global_variables_initializer())
l_out = tf.nn.relu(tf.nn.bias_add(conv,self.data_dict[name][1]))
return l_out
return tf.nn.max_pool(conv_in, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
return loss
fig, axs = plt.subplots(1, 2)
for i, path in enumerate(paths):
x = load_img(path)
length = self.sess.run(self.out, {self.tfx: x})
axs[i].imshow(x[0])
if length<80:
animal_ = 'This is a cute cat!'
else:
animal_ = 'This is a fucking tiger!'
axs[i].set_title(animal_+' body length: %.1f cm'% length)
axs[i].set_xticks(()); axs[i].set_yticks(())
plt.show()
saver = tf.train.Saver()
saver.save(self.sess, path, write_meta_graph=False)
plt.hist(tigers_y, bins=20, label='Tigers')
plt.hist(cats_y, bins=10, label='Cats')
plt.legend()
plt.xlabel('length')
plt.ion()
plt.show() #显示猫和老虎的身长直方图
ys = np.concatenate((tigers_y,cats_y), axis =0) #这里将数据混合,注意顺序
for i in range(100):
loss = vgg16.train(xs[batch_idx],ys[batch_idx])
print(i,":get the train loss/",loss)
vgg_pred = Vgg16(vgg16_npy_path = 'vgg16.npy',restore_from = 'model/transform_learning')
vgg_pred.predict(['pred_data/pred_cat.jpg','pred_data/pred_tiger.jpg'])
# Search('老虎','data/tiger',i)
# Search('猫','data/cat',i)
main_to_pred()
在这里我要再次感谢莫烦老师,希望大家可以去支持他的工作,他的迁移学习视频链接:点击打开链接
望此篇博客永存!!!
以上是 【游戏开发】Tentillflow 图片爬虫和随机森林算法省份代码表单样式 的全部内容, 来源链接: utcz.com/a/71095.html