def single_get_fc8(arr): with tf.Session() as sess: images = tf.placeholder(tf.float32, [1,224,224,3]) # 这里images的占位形状为[一次喂入的图片张数,图片长,图片宽,图片通道信息红绿蓝] vgg = vgg16.Vgg16()# 实例化vgg,运行了Vgg16类的初始化函数__ini__ vgg.forward(images) probability = sess.run(vgg.fc8, feed_dict={images: arr}) return probability
def get_fc8(img_path): img_ready = utils.load_image(img_path)# 读入待判图,并将其处理成[None,224,224,3]的形状 with tf.Session() as sess: images = tf.placeholder(tf.float32, [1,224,224,3]) # 这里images的占位形状为[一次喂入的图片张数,图片长,图片宽,图片通道信息红绿蓝] vgg = vgg16.Vgg16()# 实例化vgg,运行了Vgg16类的初始化函数__ini__ vgg.forward(images) probability = sess.run(vgg.fc8, feed_dict={images: img_ready}) return probability
def batch_get_fc8(img_ready_array): # img_ready_list = [] # for img_path in img_path_list: # img_ready_list.append(utils.load_image(img_path))# 读入待判图,并将其处理成[1,224,224,3]的形状 # img_ready_array = np.array(img_ready_list) print(img_ready_array.shape) with tf.Session() as sess: images = tf.placeholder(tf.float32, [None,224,224,3]) # 这里images的占位形状为[一次喂入的图片张数,图片长,图片宽,图片通道信息红绿蓝] vgg = vgg16.Vgg16()# 实例化vgg,运行了Vgg16类的初始化函数__ini__ vgg.forward(images) fc8_feas = sess.run(vgg.fc8, feed_dict={images: img_ready_array}) return fc8_feas
def pred_class(img_path): img_ready = utils.load_image(img_path)# 读入待判图,并将其处理成[1,224,224,3]的形状 # fig = plt.figure(u"Top-5 prediction")# 准备一张图,把运行的结果可视化。 with tf.Session() as sess: images = tf.placeholder(tf.float32, [1,224,224,3]) # 这里images的占位形状为[一次喂入的图片张数,图片长,图片宽,图片通道信息红绿蓝] vgg = vgg16.Vgg16()# 实例化vgg,运行了Vgg16类的初始化函数__ini__ vgg.forward(images) probability = sess.run(vgg.prob, feed_dict={images: img_ready}) # probability存着1000个概率,对应1000中每个类别的概率,索引key是labels中的健 # 用probability[0][i]遍历每个概率值 top5 = np.argsort(probability[0])[-1:-6:-1] # np.argsort表示对列表从小到大排序,返回索引值。这里的top5存着probability列表中5个最高概率的索引 print("top:", top5) values = []# 用来存probability中元素的值,存概率 bar_label = []# 用来存标签字典中对应的值,存名称 for n, i in enumerate(top5): # print("n:", n) # print("i:", i) values.append(probability[0][i]) bar_label.append(labels[i]) print(i, ":", labels[i], "----", utils.percent(probability[0][i])) ''' ax = fig.add_subplot(111) # fig.add_subplot(数 数 数),111表示:包含1行,包含1列,当前是第1个 ax.bar(range(len(values)), values, tick_label=bar_label, width=0.5, fc='g') # ax.bar(bar的个数,bar的值,每个bar的名字,bar宽,bar色) ax.set_ylabel(u'probabilityit') # ax.set_ylabel(" ")设置第一列的y轴名字 ax.set_title(u'Top-5') # ax.set_title(" ")设置子图名字 for a, b in zip(range(len(values)), values): ax.text(a, b+0.0005, utils.percent(b), ha='center', va='bottom', fontsize=7) # ax.text(文字x坐标,文字y坐标,文字内容,ha='center'水平方向位置,va='bottom'垂直方向位置,字号) plt.show() ''' return values[0], bar_label[0]
def fig_vectorize(): # train = pd.read_pickle(os.path.join(os.path.dirname(__file__), '..', 'output', 'train.pd'))# 读入train # train['figs_reshaped_arr'] = train['fig_path_list'].apply(lambda list: figs_reshaped_arr(list)) # pd.to_pickle(train, os.path.join(os.path.dirname(__file__), '..', 'output', 'train_reshapedfig.pd')) # test = pd.read_pickle(os.path.join(os.path.dirname(__file__), '..', 'output', 'test.pd')) # test['figs_reshaped_arr'] = test['fig_path_list'].apply(lambda list: figs_reshaped_arr(list)) # pd.to_pickle(test, os.path.join(os.path.dirname(__file__), '..', 'output', 'test_reshapedfig.pd')) with tf.Session() as sess: images = tf.placeholder(tf.float32, [None,224,224,3]) # 这里images的占位形状为[一次喂入的图片张数,图片长,图片宽,图片通道信息红绿蓝] vgg = vgg16.Vgg16()# 实例化vgg,运行了Vgg16类的初始化函数__ini__ vgg.forward(images) ## train======== ## 因为文件太大,所以分开存储 # chunk_num = 1500 # for i in range(chunk_num): # train_temp = pd.read_pickle(os.path.join(os.path.dirname(__file__), '..', 'output', 'train_reshapedfig.pd')) # chunk_size_train = math.ceil(len(train_temp) / chunk_num) # train = train_temp.iloc[i*chunk_size_train: (i+1)*chunk_size_train] # del train_temp # gc.collect() # # arr_list = [] # for arr in train['figs_reshaped_arr']: # if type(arr)!=float: # arr_list.append(arr) # del train # gc.collect() # reshaped_arr_ensemble = np.concatenate(arr_list, axis=0) # print(reshaped_arr_ensemble.shape) # print('train reshaped fig arr ensemble finished') # del arr_list # gc.collect() # # fig_feas = sess.run(vgg.fc8, feed_dict={images: reshaped_arr_ensemble}) # np.save(os.path.join(os.path.dirname(__file__), '..', 'output', 'midFile', 'train_figFC8_%d.npy'%(i)), fig_feas) # print('train feature finished %d'%(i)) # del reshaped_arr_ensemble, fig_feas # gc.collect() ## test========= chunk_num = 950 for i in range(chunk_num): test_temp = pd.read_pickle(os.path.join(os.path.dirname(__file__), '..', 'output', 'test_reshapedfig.pd')) chunk_size_test = math.ceil(len(test_temp) / chunk_num) test = test_temp.iloc[i*chunk_size_test: (i+1)*chunk_size_test] del test_temp gc.collect() arr_list = [] for arr in test['figs_reshaped_arr']: if type(arr)!=float: arr_list.append(arr) del test gc.collect() reshaped_arr_ensemble = np.concatenate(arr_list, axis=0) print(reshaped_arr_ensemble.shape) print('test reshaped fig arr ensemble finished') del arr_list gc.collect() fig_feas = sess.run(vgg.fc8, feed_dict={images: reshaped_arr_ensemble}) np.save(os.path.join(os.path.dirname(__file__), '..', 'output', 'midFile', 'test_figFC8_%d.npy'%(i)), fig_feas) print('test feature finished %d'%(i)) del reshaped_arr_ensemble, fig_feas gc.collect()