def load_data(folder, size, first=0, last=None, read_flag=cv2.IMREAD_UNCHANGED): return list( map(lambda x: cv2.resize(cv2.imread(x, read_flag), size) / 255, sorted(listdir_fullpath(folder))[first:last]))
def load_fixation_locs(folder, size=(224, 224)): maps = [] for fix_map_name in listdir_fullpath(folder): fix_map = np.array(scipy.io.loadmat(fix_map_name)['fixLocs']) y_size, x_size = fix_map.shape ones = np.where(fix_map > 0) resized_map = np.zeros(size) for y, x in zip(ones[0], ones[1]): resized_map[int(y / y_size * size[0]), int(x / x_size * size[1])] = 1 maps.append(resized_map) return np.array(maps)
def predict(heatmap_type='jet'): # creates model of neural network prediction = neural_network_model(x, keep_prob) cross_entropy = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=prediction, labels=y)) images_for_prediction = np.array( load_data(args.images, (64, 64), first=5000)) images_original = sorted(listdir_fullpath(args.images))[5000:] original_maps = np.array(load_data(args.maps, (64, 64), first=5000)) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, args.model) # restores saved model predicted_heatmaps = make_prediction(prediction, { x: images_for_prediction, keep_prob: 1.0 }) binary_maps = np.array( get_binary_fixation_maps(args.binary_maps, size=64, first=5000)) count_metrics(predicted_heatmaps, original_maps, binary_maps) i = 0 for map, img in zip(predicted_heatmaps, images_original): i += 1 #print ("working on: " + str(img.rsplit('/', 1)[1])) # saving predicted heatmaps on image visualize_heatmap(map, img, args.save_to, heatmap_type) i += 1 p = map print(str(i) + ". saving " + str(img.rsplit('/', 1)[1])) plt.imshow(p, cmap='jet') toimage(p).save("predicted_maps/" + str(img.rsplit('/', 1)[1])) plt.savefig("predicted_maps/plot_" + str(img.rsplit('/', 1)[1])) if i == 50: break
def get_binary_fixation_maps(folder, size=256, first=0, last=None): ret_maps = [] extracted_data = list( map( lambda x: { 'file': x.rsplit('/', 1)[1].replace(".mat", ".jpg"), **get_size_and_fix(loadmat(x)['s']) }, sorted(listdir_fullpath(folder))[first:last])) for data in extracted_data: #print("working on file: " + data['file']) final_map = np.zeros([size, size]) for i in range(1, 6): user_key = 'user_' + str(i) user_fix = data[user_key] final_map = apply_fixations(data['size'], data[user_key], final_map) ret_maps.append(final_map) return ret_maps
config = tf.ConfigProto(intra_op_parallelism_threads=12,\ inter_op_parallelism_threads=12, allow_soft_placement=True,\ device_count = {'CPU' : 1, 'GPU' : 0}) config.gpu_options.allow_growth = True session = tf.Session(config=config) K.set_session(session) #model = load_model('adadelta+binary.save') model = load_model(args.model) model.compile(loss=args.loss, optimizer=args.optimizer, metrics=['accuracy']) model.summary() imgs = np.array(load_data(args.images, (n, n), read_flag=cv2.IMREAD_COLOR)) img_names = sorted(listdir_fullpath(args.images)) predicted = model.predict(imgs, verbose=1) i = 0 if not os.path.exists("predicted_maps"): os.makedirs("predicted_maps") for p, img in zip(predicted, img_names): i += 1 #print(str(i) + ". saving " + str(img.rsplit('/', 1)[1])) plt.imshow(p, cmap='jet') if not os.path.exists("predicted_maps/" + args.model.rsplit("/", 2)[-1]): os.makedirs("predicted_maps/" + args.model.rsplit("/", 2)[-1]) toimage(p).save("predicted_maps/" + args.model.rsplit("/", 2)[-1] + "/" + str(img.rsplit('/', 1)[1]))
x = Conv2D(filters=1, kernel_size=5, activation='sigmoid', padding='same')(x) print(K.int_shape(x)) x = Reshape((n, n))(x) final = Model(input_layer, x) small_model = None autoencoder = None final.compile(optimizer=args.optimizer, loss=args.loss) final.summary() split = int(0.85 * args.samples) imgs_paths = sorted(listdir_fullpath(args.images)) maps_paths = sorted(listdir_fullpath(args.maps)) train_imgs_paths = imgs_paths[:split] train_maps_paths = maps_paths[:split] valid_imgs_paths = imgs_paths[split:] valid_maps_paths = maps_paths[split:] print("number of train samples: " + str(len(train_imgs_paths))) print("number of valid samples: " + str(len(valid_imgs_paths))) train_img_map_ds = load_images_and_maps(train_imgs_paths, train_maps_paths, (n, n)) valid_img_map_ds = load_images_and_maps(valid_imgs_paths, valid_maps_paths, (n, n))