<jupyter_start><jupyter_text>Post Analysis<jupyter_code># model processing hyperparameters
batch_size = 16
num_epochs = 85 #90 
steps_per_epoch = 400

# run specific constants
model_name = 'nvidia' #nvidia2
run_name = 'model={}-batch_size={}-num_epoch={}-steps_per_epoch={}'.format(model_name,
                                                                          batch_size,
                                                                          num_epochs,
                                                                          steps_per_epoch)
ASSETS_PATH = '/assets'


# hyperparameters
batch_size = 16
num_epochs = 25 #100 #90 
steps_per_epoch = 400

# run specific constants
model_name = 'nvidia' #nvidia2

assets_filepath = os.path.join(ASSETS_PATH, '85-epoch-run')
weights_loc = os.path.join(assets_filepath,'model-weights-Vtest3-85.h5')
#history_loc=  os.path.join(assets_filepath,'history.p')
#tensorboard_loc = os.path.join(assets_filepath, run_name)



#history = pickle.load(open(history_loc, "rb" ))
model.load_weights('model-weights-Vtest3-85.h5')
adam = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer = adam, loss = 'mse')
CLEAN_DATA_PATH = ''
test_meta = pd.read_csv('test2_meta.csv')
#assert(test_meta.shape[0] == test_frames)
assert(test_meta.shape[1] == 3)
print('shape: ', test_meta.shape)

val_size = len(test_meta.index)
valid_generator = generate_validation_data(test_meta)
val_score = model.evaluate_generator(valid_generator, steps=val_size)
print('val score:', val_score)
def make_predictions(data):
    for idx in tqdm(range(1, len(data.index)-1)):
        row_now = data.iloc[[idx]].reset_index()
        row_prev = data.iloc[[idx - 1]].reset_index()
        row_next = data.iloc[[idx + 1]].reset_index()
        
        time_now = row_now['image_index'].values[0]
        time_prev = row_prev['image_index'].values[0]
        time_next = row_next['image_index'].values[0]
        
        if abs(time_now - time_prev) == 1 and time_now > time_prev:
            row1 = row_prev
            row2 = row_now
        elif abs(time_next - time_now) == 1 and time_next > time_now:
            row1 = row_now
            row2 = row_next
        else:
            print('Error generating row')
        
        x1, y1 = preprocess_image_valid_from_path(row1['image_path'].values[0], row1['speed'].values[0])
        x2, y2 = preprocess_image_valid_from_path(row2['image_path'].values[0], row2['speed'].values[0])
        
        img_diff = opticalFlowDense(x1, x2)
        img_diff = img_diff.reshape(1, img_diff.shape[0], img_diff.shape[1], img_diff.shape[2])
        y = np.mean([y1, y2])

        prediction = model.predict(img_diff)
        error = abs(prediction-y2)
        
        
        #print(prediction, error, time_now)
        data.loc[data['image_index']==time_now, 'predicted_speed'] = prediction[0][0]
        data.loc[data['image_index']==time_now, 'error'] = error[0][0]
make_predictions(test_meta)
print(mean_squared_error(test_meta.iloc[1:-1, 3], 
                         test_meta.iloc[1:-1, 4]))
fig, ax = plt.subplots(figsize=(20,10))
plt.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['speed']], 'go')
plt.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['predicted_speed']], 'bx')
plt.xlabel('image_index (or time since start)')
plt.ylabel('speed')
plt.title('Predicted vs actual on validation data')
plt.legend(['validation speed', 'predicted speed'], loc='upper right')
plt.show()
plt.close()
fig, ax1 = plt.subplots(figsize=(20,10))
ax1.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['speed']], 'go', alpha=0.1)

ax2 = ax1.twinx()
ax2.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['error']], 'co')

ax1.set_xlabel('image_index (or time since start)')
ax1.set_ylabel('speed')
ax2.set_ylabel('prediction error')
plt.title('Predicted vs actual on validation data')
plt.legend(['prediction error'], loc='upper right')
plt.show()
plt.close()
def error_analysis(error_thresh):
    fig, ax = plt.subplots(figsize=(20,10))
    ax.plot(test_meta.sort_values(['image_index'])[['image_index']], 
             test_meta.sort_values(['image_index'])[['speed']], 'go', alpha=0.4)


    ax.plot(test_meta[test_meta['error']>error_thresh].sort_values(['image_index'])[['image_index']], 
             test_meta[test_meta['error']>error_thresh].sort_values(['image_index'])[['speed']], 'rx')


    ax.set_xlabel('image_index (or time since start)')
    ax.set_ylabel('speed')
    plt.title('Error analysis on validation data')
    plt.legend(['Ground truth', 'Incorrect prediction'], loc='upper right')
    plt.show()

interact(error_analysis, error_thresh=(0,5, 0.1))<jupyter_output><empty_output><jupyter_text>Prediction on testing with ground truth<jupyter_code>test_meta.head()
window_size = 25

test_meta['smooth_predicted_speed'] = test_meta['predicted_speed'].rolling(window = 25).mean()
test_meta['smooth_error'] = test_meta.apply(lambda x: x['smooth_predicted_speed'] - x['speed'], axis=1)
test_meta['smooth_predicted_speed'] = test_meta.apply(lambda x: 
                                                        x['predicted_speed'] if np.isnan(x['smooth_predicted_speed'])
                                                       else x['smooth_predicted_speed'],axis=1)
test_meta['smooth_error'] = test_meta.apply(lambda x: x['error'] if np.isnan(x['smooth_error'])
                                                       else x['smooth_error'],axis=1)

fig, ax = plt.subplots(figsize=(20,10))
plt.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['predicted_speed']], 'bx')
plt.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['smooth_predicted_speed']], 'g.')
plt.plot(test_meta.sort_values(['image_index'])[['image_index']], 
         test_meta.sort_values(['image_index'])[['speed']], 'r.')
plt.xlabel('image_index (or time since start)')
plt.ylabel('speed')
plt.title('Predicted vs actual on test data')
plt.legend(['predicted speed', (str(window_size) +' window average'), 'ground truth'], loc='upper right')
plt.show()
plt.close()
print(mean_squared_error(test_meta.iloc[1:-1, 2], 
                         test_meta.iloc[1:-1, 3]))
print(test_meta.iloc[1:-1, 3])<jupyter_output>1        5.232239
2        9.141287
3        5.023733
4        6.487329
5       10.341707
6        6.477208
7        5.829556
8        7.044203
9        9.790969
10       5.275253
11       6.056460
12       8.642632
13       5.065383
14       4.804243
15       4.398646
16      13.002735
17       4.745164
18      11.449855
19      11.800059
20       4.676222
21       4.587283
22       7.636438
23      19.439163
24       6.697103
25       9.026002
26       7.264797
27       3.984258
28       4.911417
29       9.130272
30       5.524652
          ...    
8585     5.581154
8586     0.187491
8587    12.893432
8588     0.982247
8589    17.560961
8590     7.784309
8591    13.546109
8592    11.895207
8593     6.768760
8594    12.657473
8595     7.643412
8596     5.102735
8597     7.149134
8598    13.242188
8599     9.540568
8600     9.206415
8601    10.622692
8602    17.314091
8603    12.588028
8604     0.724840
8605     4.581952
8606     1.581216
8607    10.112781
8608    11.285285
8609     6[...]
{'fraction_non_alphanumeric': 0.14846123365473518, 'fraction_numerical': 0.09113723418306696, 'mean_word_length': 3.049197860962567, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '620176', 'license': 'MIT', 'n_tokens_mistral': 3042, 'n_tokens_neox': 2652, 'n_words': 443, 'path': 'Model_3D/speed_cnn.ipynb', 'repo_name': 'UCSD-ECE141/Vehicle-motion-based-on-vision', 'size': '7571'}