def generate_fake_data(time_series_size, data_cnt): model_file = 'models/' + str(time_series_size) + '_' + str( p_dimension) + '_dcgan.h5' model = tf.keras.models.load_model(model_file) p = np.random.randint(3, size=(data_cnt, 5)) + np.ones((data_cnt, 5)) test_labels = np.array( od.convert_personality_to_class_label(p, p_dimension)) z_input = tf.random.normal([data_cnt, latent_dim]) predictions_all = model.predict([z_input, test_labels]) xy_pupil = predictions_all[0][:, :, :, 0] xy = predictions_all[0][:, :, 0:2, 0] blinks = decoder(predictions_all[1]).numpy().reshape( data_cnt, time_series_size, 1) if feature_size > 3: x = np.concatenate((xy_pupil, blinks), axis=2) elif feature_size == 3: x = xy_pupil else: x = xy return x, test_labels
def plot_predictions(model, padding_start, padding_end, noise): # def plot_predictions(model, noise): test_labels1 = np.array([ od.convert_personality_to_class_label([[1, 1, 1, 1, 1]], p_dimension) ]) test_labels2 = np.array([ od.convert_personality_to_class_label([[2, 2, 2, 2, 2]], p_dimension) ]) test_labels3 = np.array([ od.convert_personality_to_class_label([[3, 3, 3, 3, 3]], p_dimension) ]) # predictions1 = model.predict(noise) # predictions2 = model.predict(noise) # predictions3 = model.predict(noise) predictions1 = model.predict( [padding_start, padding_end, noise, test_labels1]) predictions2 = model.predict( [padding_start, padding_end, noise, test_labels2]) predictions3 = model.predict( [padding_start, padding_end, noise, test_labels3]) # map them back to 0 1 region vx1 = [(i + 1) / 2.0 for i in predictions1[:, :, 0]][0] vx2 = [(i + 1) / 2.0 for i in predictions2[:, :, 0]][0] vx3 = [(i + 1) / 2.0 for i in predictions3[:, :, 0]][0] plt.subplot(211) plt.title("X") plt.plot(np.arange(0, time_series_size), vx1, c='b') plt.plot(np.arange(0, time_series_size), vx2, c='g') plt.plot(np.arange(0, time_series_size), vx3, c='r') vy1 = [(i + 1) / 2.0 for i in predictions1[:, :, 1]][0] vy2 = [(i + 1) / 2.0 for i in predictions2[:, :, 1]][0] vy3 = [(i + 1) / 2.0 for i in predictions3[:, :, 1]][0] plt.subplot(212) plt.title("Y") plt.plot(np.arange(0, time_series_size), vy1, c='b') # 1 plt.plot(np.arange(0, time_series_size), vy2, c='g') # 2 plt.plot(np.arange(0, time_series_size), vy3, c='r') # 3 plt.show()
def generate_and_save_data(model, p, iter_cnt, will_plot=False): test_labels = np.array( [od.convert_personality_to_class_label([p], p_dimension)]) noise_padding_start = None for it in range(iter_cnt): noise = tf.random.normal([1, latent_dim]) noise_padding_end = tf.random.normal( [1, latent_padding, feature_size * 2]) # this will shift if noise_padding_start is None: noise_padding_start = tf.random.normal( [1, latent_padding, feature_size * 2]) predictions = model.predict( [noise_padding_start, noise_padding_end, noise, test_labels]) noise_padding_start = noise_padding_end # shift padding for the next iteration if it == 0: predictions_all = predictions[0] else: clipped_start = latent_padding # each time clip the beginning part predictions_all = np.concatenate( (predictions_all, predictions[0][clipped_start:, :]), axis=0) # predictions_all = np.concatenate((predictions_all, predictions[0]), axis=0) # map back to 0 1 region vx = [(i + 1) / 2.0 for i in predictions_all[:, 0]] vy = [(i + 1) / 2.0 for i in predictions_all[:, 1]] p_str = conf.personality_name[int(p_dimension)] f_ext = '_'.join(map(str, p)) + '_dim_' + p_str v = list(zip(vx, vy)) with open('out/gaze_positions_' + f_ext + '.csv', 'w') as fp: np.savetxt(fp, v, delimiter=',') if will_plot: plt.subplots_adjust(hspace=0.5) plt.subplot(211) plt.title('X ' + f_ext) plt.plot(np.arange(0, len(vx)), vx, c='magenta') plt.subplot(212) plt.title('Y ' + f_ext) plt.plot(np.arange(0, len(vy)), vy, c='cyan') for it in range(0, iter_cnt * 2): plt.axvline(it * int(time_series_size / 2), color='g', lw=0.3) plt.show()
n_classes = 243 # all personalities else: n_classes = 3 # one personality dimension model_file = 'models/' + str(time_series_size) + '_' + str( p_dimension) + '_cnngan.h5' tf.compat.v1.enable_eager_execution() (x, p_values) = od.load_data(time_series_size) x_train = x[:, :, 0:2] x_train = 2 * x_train - 1 p_values = p_values p_labels = od.convert_personality_to_class_label(p_values, p_dimension) # slice along the 1st dimension, i.e. each row is a slice train_data = tf.data.Dataset.from_tensor_slices( (x_train, p_labels)).shuffle(buffer_size=60000).batch(conf.batch_size) def plot_real_data(): x_vals = x[:, :, 0] y_vals = x[:, :, 1] plt.subplot(211) plt.plot(range(time_series_size * 4), np.reshape(x_vals[0:4], (time_series_size * 4))) plt.subplot(212) plt.plot(range(time_series_size * 4), np.reshape(y_vals[0:4], (time_series_size * 4)))
def plot_predictions(model, test_input): test_labels1 = np.repeat(np.array([ od.convert_personality_to_class_label([[1, 1, 1, 1, 1]], p_dimension) ]), test_input.shape[0], axis=0) test_labels2 = np.repeat(np.array([ od.convert_personality_to_class_label([[2, 2, 2, 2, 2]], p_dimension) ]), test_input.shape[0], axis=0) test_labels3 = np.repeat(np.array([ od.convert_personality_to_class_label([[3, 3, 3, 3, 3]], p_dimension) ]), test_input.shape[0], axis=0) predictions1 = model.predict([test_input, test_labels1]) predictions2 = model.predict([test_input, test_labels2]) predictions3 = model.predict([test_input, test_labels3]) # map them back to 0 1 region vx1 = [(i + 1) / 2.0 for i in predictions1[0][:, :, 0]][0] vx2 = [(i + 1) / 2.0 for i in predictions2[0][:, :, 0]][0] vx3 = [(i + 1) / 2.0 for i in predictions3[0][:, :, 0]][0] plt.title("X") plt.plot(np.arange(0, time_series_size), vx1) plt.plot(np.arange(0, time_series_size), vx2) plt.plot(np.arange(0, time_series_size), vx3) plt.show() if feature_size > 1: vy1 = [(i + 1) / 2.0 for i in predictions1[0][:, :, 1]][0] vy2 = [(i + 1) / 2.0 for i in predictions2[0][:, :, 1]][0] vy3 = [(i + 1) / 2.0 for i in predictions3[0][:, :, 1]][0] plt.title("Y") plt.plot(np.arange(0, time_series_size), vy1, c='b') # 1 plt.plot(np.arange(0, time_series_size), vy2, c='g') # 2 plt.plot(np.arange(0, time_series_size), vy3, c='r') # 3 plt.show() if conf.plot_stats: matplotlib.rcParams.update({'axes.titlesize': 18}) plt.rc('axes', labelsize=16) if p_dimension is None: plt.title("OCEAN") else: plt.title(conf.personality_name[p_dimension]) plt.xlabel('x') plt.ylabel('y') plt.scatter(vx1, vy1, c='b', label='low') plt.scatter(vx2, vy2, c='g', label='med') plt.scatter(vx3, vy3, c='r', label='high') plt.legend() plt.show() if feature_size > 2: p1 = predictions1[0][:, :, 2][0] p2 = predictions2[0][:, :, 2][0] p3 = predictions2[0][:, :, 2][0] if conf.plot_stats: d1 = predictions1[0][:, :, 2].flatten() d2 = predictions2[0][:, :, 2].flatten() d3 = predictions3[0][:, :, 2].flatten() d1 = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in d1] d2 = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in d2] d3 = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in d3] d_plot = [d1, d2, d3] if p_dimension is None: plt.title("OCEAN") else: plt.title(conf.personality_name[p_dimension]) bp = plt.boxplot(d_plot, patch_artist=True) colors = ['blue', 'green', 'red'] for patch, color in zip(bp['boxes'], colors): patch.set_facecolor(color) plt.xticks([1, 2, 3], ["low", "med", "high"]) plt.show() vp1 = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in p1] vp2 = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in p2] vp3 = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in p3] vp1 = list(zip(vp1, vp1)) vp2 = list(zip(vp2, vp2)) vp3 = list(zip(vp3, vp3)) plt.title('Avg. pupil size') plt.ylim(2, 4) plt.plot(np.arange(0, time_series_size), vp1, c='b', label='low') plt.plot(np.arange(0, time_series_size), vp2, c='g', label='med') plt.plot(np.arange(0, time_series_size), vp3, c='r', label='high') plt.show() if feature_size > 3: blinks1 = decoder(predictions1[1])[0] blinks2 = decoder(predictions2[1])[0] blinks3 = decoder(predictions3[1])[0] threshold1 = np.max(blinks1) * 0.5 threshold2 = np.max(blinks2) * 0.5 threshold3 = np.max(blinks3) * 0.5 blinks1 = [1 if i > threshold1 else 0 for i in blinks1] blinks2 = [1 if i > threshold2 else 0 for i in blinks1] blinks3 = [1 if i > threshold3 else 0 for i in blinks1] plt.title("Blinks") plt.plot(np.arange(0, time_series_size), blinks1, c='b') # 1 plt.plot(np.arange(0, time_series_size), blinks2, c='g') # 2 plt.plot(np.arange(0, time_series_size), blinks3, c='r') # 3 plt.show()
def generate_and_save_data(model, p, motion_length, will_plot=False, p_str=None): iter_cnt = int(motion_length / time_series_size) prev_half_noise = None decoded_predictions = np.array([]) for it in range(iter_cnt): test_labels = np.array( [od.convert_personality_to_class_label([p], p_dimension)]) half_noise = tf.random.normal([1, half_latent_dim]) if prev_half_noise is None: prev_half_noise = tf.random.normal([1, half_latent_dim]) noise = tf.concat([prev_half_noise, half_noise], axis=1) # noise = tf.random.normal([1, latent_dim]) predictions_all = model.predict([noise, test_labels]) prev_half_noise = half_noise if it == 0: predictions0 = predictions_all[0][0] else: predictions0 = np.concatenate( (predictions0, predictions_all[0][0]), axis=0) predictions1 = predictions_all[1] if feature_size > 3: decoded_predictions_partial = np.array(decoder(predictions1))[0] decoded_predictions = np.concatenate( (decoded_predictions, decoded_predictions_partial), axis=0) vx = [(i + 1) / 2.0 for i in predictions0[:, 0]] vy = [(i + 1) / 2.0 for i in predictions0[:, 1]] vx_s = smooth(vx, 5) vy_s = smooth(vy, 5) eps = 1e-3 i = 0 while abs(vx_s[i] - vx[i]) > eps: # vx_s[i] = vx[i] i = i + 1 i = -1 while abs(vx_s[i] - vx[i]) > eps: # vx_s[i] = vx[i] i = i - 1 eps = 1e-3 i = 0 while abs(vy_s[i] - vy[i]) > eps: # vy_s[i] = vy[i] i = i + 1 i = -1 while abs(vy_s[i] - vy[i]) > eps: # vy_s[i] = vy[i] i = i - 1 v = list(zip(vx_s, vy_s)) if will_plot: plt.title("x-dashed, y normal") plt.plot(np.arange(0, motion_length), vx, linestyle='dashed') plt.plot(np.arange(0, motion_length), vy) plt.axvline(150) plt.axvline(300) plt.axvline(450) plt.show() if p_str: f_ext = '_'.join(map(str, p)) + '_dim_' + p_str else: f_ext = '_'.join(map(str, p)) with open('out/gaze_positions_' + f_ext + '.csv', 'w') as fp: np.savetxt(fp, v, delimiter=',') if feature_size > 2: vp = [(max_pupils - min_pupils) * (i + 1) / 2.0 + min_pupils for i in predictions0[:, 2]] vp = list(zip(vp, vp)) if will_plot: plt.title("Pupils") plt.plot(np.arange(0, motion_length), vp) plt.axvline(150) plt.axvline(300) plt.axvline(450) plt.show() with open('out/pupil_diameter_' + f_ext + '.csv', 'w') as fp: np.savetxt(fp, vp, delimiter=',')