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 taka_generator_fig_text_batch(text_seq_list, all_fig_path_list, label_list, batch_size, fig_resize_shape, nolabel=False): fig_mean = np.load( os.path.join(os.path.dirname(__file__), '..', 'output', 'train_figmean.npy')) length = len(text_seq_list) index = 0 # index逐渐递增,但是值每次都对length取模 while True: index_list = [] for i in range(batch_size): index_list.append(index + i) #label label = np.array(list(label_list.iloc[index_list]), dtype='int32') label = label.reshape((batch_size, 1)) #构造文本的输入array text_seq = text_seq_list[index_list] text_arr = np.array(list(text_seq), dtype='int32') # 构造图像的输入array fig_path_list_batch = all_fig_path_list.iloc[index_list] user_fig_arr = np.zeros(shape=(batch_size, 10, fig_resize_shape, fig_resize_shape, 3)) for batch_index in range(len(fig_path_list_batch)): fig_path_list = fig_path_list_batch.iloc[batch_index] for i in range(len(fig_path_list)): path = fig_path_list[i] try: fig_arr = load_image(path) if fig_arr.shape != (fig_resize_shape, fig_resize_shape, 3): print('?:', path) else: fig_arr = fig_arr - fig_mean ##################### fig归一化 user_fig_arr[batch_index, i] = fig_arr except BaseException as e: pass index = (index + batch_size) % length if nolabel: yield [text_arr, user_fig_arr] else: yield ([text_arr, user_fig_arr], label)
def figs_reshaped_arr(fig_path):# 传入的是某个用户的所有图片的路径 if len(fig_path)==0:# 如果某个用户拥有的图片数为0 return np.nan reshape_list = [] for path in fig_path: try: img_reshaped = utils.load_image(path) reshape_list.append(img_reshaped) except OSError as e: print('img cannot open:',path,e) pass if len(reshape_list)==0: return np.nan arr = np.zeros(shape=[len(reshape_list), 224, 224, 3]) for i,reshaped in enumerate(reshape_list): arr[i,:,:,:] = reshaped return arr
def generator_fig_hue_hist_batch(all_fig_path_list, label_list, hist_count, batch_size, fig_resize_shape, nolabel=False): length = len(all_fig_path_list) index = 0 while True: index_list = [] for i in range(batch_size): index_list.append(index + i) #label label = np.array(list(label_list.iloc[index_list]), dtype='int32') label = label.reshape((batch_size, 1)) # 构造图像的输入array fig_path_list_batch = all_fig_path_list.iloc[index_list] user_fig_arr = np.zeros(shape=(batch_size, 10, hist_count)) for batch_index in range(len(fig_path_list_batch)): fig_path_list = fig_path_list_batch.iloc[batch_index] for i in range(len(fig_path_list)): path = fig_path_list[i] try: fig_arr = load_image(path) if fig_arr.shape != (fig_resize_shape, fig_resize_shape, 3): print('?:', path) else: fig_hsv = rgb2hsv(fig_arr) fig_hue = fig_hsv[:, :, 0] hist_arr = list2hist(fig_hue.ravel(), hist_count) user_fig_arr[batch_index, i] = hist_arr except BaseException as e: pass index = (index + batch_size) % length if nolabel: yield user_fig_arr else: yield (user_fig_arr, label)
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 generator_fig_text_senti_multioutput_batch(text_seq_list, all_fig_path_list, label_list, batch_size, fig_resize_shape, nolabel=False): fig_mean = np.load( os.path.join(os.path.dirname(__file__), '..', 'output', 'train_figmean.npy')) dic = json.load(open(SENTI_DIC_PATH)) length = len(text_seq_list) index = 0 # index逐渐递增,但是值每次都对length取模 while True: index_list = [] for i in range(batch_size): index_list.append(index + i) #label label = np.array(list(label_list.iloc[index_list]), dtype='int32') label = label.reshape((batch_size, 1)) #构造文本的输入array text_seq = text_seq_list.iloc[index_list] text_arr = np.array(list(text_seq), dtype='int32') text_senti = np.zeros((text_arr.shape[0], text_arr.shape[1], text_arr.shape[2])) # (batchsize, 100, 50) for k in range(len(text_arr)): sample = text_arr[k] for i in range(sample.shape[0]): for j in range(sample.shape[1]): num = sample[i][j] if dic.__contains__(num): print('contains', num) text_senti[k][i][j] = dic[num] text_senti = text_senti.reshape( (text_arr.shape[0], text_arr.shape[1], text_arr.shape[2], 1)) # 构造图像的输入array fig_path_list_batch = all_fig_path_list.iloc[index_list] user_fig_arr = np.zeros(shape=(batch_size, 10, fig_resize_shape, fig_resize_shape, 3)) for batch_index in range(len(fig_path_list_batch)): fig_path_list = fig_path_list_batch.iloc[batch_index] for i in range(len(fig_path_list)): path = fig_path_list[i] try: fig_arr = load_image(path) if fig_arr.shape != (fig_resize_shape, fig_resize_shape, 3): print('?:', path) else: fig_arr = fig_arr - fig_mean ##################### fig归一化 user_fig_arr[batch_index, i] = fig_arr except BaseException as e: pass index = (index + batch_size) % length if nolabel: yield [text_arr, text_senti, user_fig_arr] else: yield ([text_arr, text_senti, user_fig_arr], [label, label, label])