Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Python 3 Support #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions action_classifier/action_classifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import numpy as np
import pickle
import numpy as np
from __future__ import print_function

folder = "../Youtube2Text/youtubeclips-dataset/"

Expand Down Expand Up @@ -36,7 +37,7 @@
num_actions = video_action_vectors[video_action_vectors.keys()[0]].shape[0]
feature_size = video_frame_features[video_frame_features.keys()[0]].shape[1]

print "Training action classifier model ..."
print("Training action classifier model ...")

# Define the action classifier model
from keras.models import Sequential
Expand All @@ -52,7 +53,7 @@

action_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# Save a visualization of the model and print summary
# Save a visualization of the model and printsummary
#from keras.utils import plot_model
#plot_model(action_model, to_file='action_classifier_model.png')
action_model.summary()
Expand All @@ -69,8 +70,8 @@

from sklearn.metrics import hamming_loss

preds_binarized = preds
preds_binarized = preds
preds_binarized[preds>=0.5] = 1
preds_binarized[preds<0.5] = 0

print "Hamming Loss: ", hamming_loss(Y_test, preds_binarized)
print("Hamming Loss: ", hamming_loss(Y_test, preds_binarized))
5 changes: 3 additions & 2 deletions action_classifier/action_classifier_data_prep.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pickle
import sys
from __future__ import print_function

folder = "../YouTube2Text/youtubeclips-dataset/"
actions_file = "../tag_generator/action_long.txt"
Expand All @@ -17,9 +18,9 @@
actions = []
for word,count in actions_all:
if int(count) >= MIN_COUNT:
actions.append(word)
actions.append(word)

print "Using " + str(len(actions)) + " actions out of total " + str(len(actions_all)) + " actions"
print("Using " + str(len(actions)) + " actions out of total " + str(len(actions_all)) + " actions")

# read in cleaned up captions
with open(folder+captions_file) as f:
Expand Down
8 changes: 5 additions & 3 deletions action_classifier/action_vector_predict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import numpy as np
import pickle
import numpy as np
from __future__ import print_function


folder = "../Youtube2Text/youtubeclips-dataset/"

Expand Down Expand Up @@ -60,7 +62,7 @@ def action_vector_to_list(action_vec, p):
return [actions[i] for i in indices]


# print "p, Hamming Loss, Avg Labels, Avg Correct Labels"
# print("p, Hamming Loss, Avg Labels, Avg Correct Labels")
# for p in np.arange(0.0, 0.6, 0.02):
# # Compute Hamming Loss
# total_predicted = 0
Expand All @@ -73,10 +75,10 @@ def action_vector_to_list(action_vec, p):
# hamming_loss = float(total_predicted - total_correct)/total_predicted
# avg_labels = total_predicted/float(len(test))
# avg_correct_labels = avg_labels*(1 - hamming_loss)
# print p, hamming_loss, avg_labels, avg_correct_labels
# print(p, hamming_loss, avg_labels, avg_correct_labels)


# TODO
# TODO
# Create crude sentences for checking BLEU/METEOR
for idx,video in enumerate(test):
predicted_labels = set(action_vector_to_list(preds[idx],0.25))
Expand Down
15 changes: 8 additions & 7 deletions advanced_tag_models/lstm_vgg_tag_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from keras.applications.resnet50 import preprocess_input
from keras.callbacks import CSVLogger, ModelCheckpoint, ReduceLROnPlateau
from keras import regularizers
from __future__ import print_function


import numpy as np
Expand Down Expand Up @@ -74,7 +75,7 @@ def preprocess_image(img_path):

for video in sorted(videos):
if count % 100 == 0:
print int(100*round(count/float(len(videos)),2)), "% Data Loaded"
print(int(100*round(count/float(len(videos)),2)), "% Data Loaded")
video_name = (video.split("/")[-1]).split(".")[0] # Extract video name from path
fps = video_fps[video_name]
frame_files = sorted(glob(video +"/*.jpg"))
Expand All @@ -88,7 +89,7 @@ def preprocess_image(img_path):
frame_data.append(preprocess_image(frame_file)[0])
actual_frame_length = len(frame_data)
# If Video is shorter than 8 seconds repeat the short video
if len(frame_data) < NUM_FRAMES:
if len(frame_data) < NUM_FRAMES:
if NUM_FRAMES/len(frame_data) > 1: # Video is < 1/2 of 8 Seconds
num_repeats = NUM_FRAMES/len(frame_data) - 1
for _ in range(num_repeats):
Expand All @@ -99,7 +100,7 @@ def preprocess_image(img_path):
for itr in range(0, NUM_FRAMES -len(frame_data)):
frame_data.append(frame_data[itr])
if len(frame_data) != NUM_FRAMES:
print og_frame_length, num_repeats, dup_frame_length, len(frame_data)
print(og_frame_length, num_repeats, dup_frame_length, len(frame_data))
raise Exception, 'Incorrect number of frames sampled'
frame_data = np.array(frame_data)
if video_name in test:
Expand Down Expand Up @@ -215,13 +216,13 @@ def preprocess_image(img_path):
test_preds_binarized[test_preds>=0.5] = 1
test_preds_binarized[test_preds<0.5] = 0

print "Hamming Loss: ", hamming_loss(test_tags, test_preds_binarized)
print("Hamming Loss: ", hamming_loss(test_tags, test_preds_binarized))

augmented_test_preds_binarized = copy.deepcopy(augmented_test_preds)
augmented_test_preds_binarized[augmented_test_preds>=0.5] = 1
augmented_test_preds_binarized[augmented_test_preds<0.5] = 0

print "Hamming Loss For Augmented Preds: ", hamming_loss(test_tags, augmented_test_preds_binarized)
print("Hamming Loss For Augmented Preds: ", hamming_loss(test_tags, augmented_test_preds_binarized))

# Calculate Micro Averaged Precision
# For each class
Expand All @@ -235,7 +236,7 @@ def preprocess_image(img_path):
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], _ = precision_recall_curve(test_tags.ravel(), test_preds.ravel())
average_precision["micro"] = average_precision_score(test_tags, test_preds, average="micro")
print 'Average precision score, micro-averaged over all classes:', average_precision["micro"]
print('Average precision score, micro-averaged over all classes:', average_precision["micro"])

# Plot uAP v Recall curve
plt.switch_backend("agg")
Expand All @@ -261,7 +262,7 @@ def preprocess_image(img_path):
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], _ = precision_recall_curve(test_tags.ravel(), augmented_test_preds.ravel())
average_precision["micro"] = average_precision_score(test_tags, augmented_test_preds, average="micro")
print 'Average precision score, micro-averaged over all classes:', average_precision["micro"]
print'Average precision score, micro-averaged over all classes:', average_precision["micro"]

# Plot uAP v Recall curve
plt.switch_backend("agg")
Expand Down
17 changes: 9 additions & 8 deletions advanced_tag_models/lstm_vgg_tag_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from keras.applications.resnet50 import preprocess_input
from keras.callbacks import CSVLogger, ModelCheckpoint
from keras import regularizers
from __future__ import print_function


import numpy as np
Expand Down Expand Up @@ -74,7 +75,7 @@ def preprocess_image(img_path):

for video in sorted(videos):
if count % 100 == 0:
print int(100*round(count/float(len(videos)),2)), "% Data Loaded"
print(int(100*round(count/float(len(videos)),2)), "% Data Loaded")
video_name = (video.split("/")[-1]).split(".")[0] # Extract video name from path
fps = video_fps[video_name]
frame_files = sorted(glob(video +"/*.jpg"))
Expand All @@ -88,7 +89,7 @@ def preprocess_image(img_path):
frame_data.append(preprocess_image(frame_file)[0])
actual_frame_length = len(frame_data)
# If Video is shorter than 8 seconds repeat the short video
if len(frame_data) < NUM_FRAMES:
if len(frame_data) < NUM_FRAMES:
if NUM_FRAMES/len(frame_data) > 1: # Video is < 1/2 of 8 Seconds
num_repeats = NUM_FRAMES/len(frame_data) - 1
for _ in range(num_repeats):
Expand All @@ -99,7 +100,7 @@ def preprocess_image(img_path):
for itr in range(0, NUM_FRAMES -len(frame_data)):
frame_data.append(frame_data[itr])
if len(frame_data) != NUM_FRAMES:
print og_frame_length, num_repeats, dup_frame_length, len(frame_data)
print(og_frame_length, num_repeats, dup_frame_length, len(frame_data))
raise Exception, 'Incorrect number of frames sampled'
frame_data = np.array(frame_data)
if video_name in test:
Expand Down Expand Up @@ -205,13 +206,13 @@ def preprocess_image(img_path):
test_preds_binarized[test_preds>=0.5] = 1
test_preds_binarized[test_preds<0.5] = 0

print "Hamming Loss: ", hamming_loss(test_tags, test_preds_binarized)
print("Hamming Loss: ", hamming_loss(test_tags, test_preds_binarized))

augmented_test_preds_binarized = copy.deepcopy(augmented_test_preds)
augmented_test_preds_binarized[augmented_test_preds>=0.5] = 1
augmented_test_preds_binarized[augmented_test_preds<0.5] = 0

print "Hamming Loss For Augmented Preds: ", hamming_loss(test_tags, augmented_test_preds_binarized)
print("Hamming Loss For Augmented Preds: ", hamming_loss(test_tags, augmented_test_preds_binarized))

# Calculate Micro Averaged Precision
# For each class
Expand All @@ -225,7 +226,7 @@ def preprocess_image(img_path):
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], _ = precision_recall_curve(test_tags.ravel(), test_preds.ravel())
average_precision["micro"] = average_precision_score(test_tags, test_preds, average="micro")
print 'Average precision score, micro-averaged over all classes:', average_precision["micro"]
print('Average precision score, micro-averaged over all classes:', average_precision["micro"])

# Plot uAP v Recall curve
plt.switch_backend("agg")
Expand All @@ -251,7 +252,7 @@ def preprocess_image(img_path):
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], _ = precision_recall_curve(test_tags.ravel(), augmented_test_preds.ravel())
average_precision["micro"] = average_precision_score(test_tags, augmented_test_preds, average="micro")
print 'Average precision score, micro-averaged over all classes:', average_precision["micro"]
print('Average precision score, micro-averaged over all classes:', average_precision["micro"])

# Plot uAP v Recall curve
plt.switch_backend("agg")
Expand All @@ -264,4 +265,4 @@ def preprocess_image(img_path):
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Average precision score, micro-averaged over all classes: AUC={0:0.2f}'.format(average_precision["micro"]))
plt.savefig('PR_Curve_'+results.tag_type+'_bidirectional_augmented.png')
plt.savefig('PR_Curve_'+results.tag_type+'_bidirectional_augmented.png')
19 changes: 10 additions & 9 deletions advanced_tag_models/predict_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import numpy as np
from glob import glob
import h5py
import pickle
import pickle
from __future__ import print_function

import argparse

Expand Down Expand Up @@ -62,7 +63,7 @@ def preprocess_image(img_path):

for video in sorted(videos):
if count % 100 == 0:
print int(100*round(count/float(len(videos)),2)), "% Data Loaded"
print(int(100*round(count/float(len(videos)),2)), "% Data Loaded")
video_name = (video.split("/")[-1]).split(".")[0] # Extract video name from path
fps = video_fps[video_name]
frame_files = sorted(glob(video +"/*.jpg"))
Expand All @@ -76,7 +77,7 @@ def preprocess_image(img_path):
frame_data.append(preprocess_image(frame_file)[0])
actual_frame_length = len(frame_data)
# If Video is shorter than 8 seconds repeat the short video
if len(frame_data) < NUM_FRAMES:
if len(frame_data) < NUM_FRAMES:
if NUM_FRAMES/len(frame_data) > 1: # Video is < 1/2 of 8 Seconds
num_repeats = NUM_FRAMES/len(frame_data) - 1
for _ in range(num_repeats):
Expand All @@ -87,7 +88,7 @@ def preprocess_image(img_path):
for itr in range(0, NUM_FRAMES -len(frame_data)):
frame_data.append(frame_data[itr])
if len(frame_data) != NUM_FRAMES:
print og_frame_length, num_repeats, dup_frame_length, len(frame_data)
print(og_frame_length, num_repeats, dup_frame_length, len(frame_data))
raise Exception, 'Incorrect number of frames sampled'
frame_data = np.array(frame_data)
if video_name in test:
Expand Down Expand Up @@ -127,7 +128,7 @@ def preprocess_image(img_path):
layer.trainable = False

encoded_frame_sequence = TimeDistributed(convnet_model)(video_input)
encoded_video = LSTM(256)(encoded_frame_sequence)
encoded_video = LSTM(256)(encoded_frame_sequence)
output = Dense(NUM_ACTIONS, activation='sigmoid')(encoded_video)

action_model = Model(inputs=video_input, outputs=output)
Expand All @@ -149,11 +150,11 @@ def preprocess_image(img_path):

# from sklearn.metrics import hamming_loss

# preds_binarized = preds
# preds_binarized = preds
# preds_binarized[preds>=0.5] = 1
# preds_binarized[preds<0.5] = 0

# print "Hamming Loss: ", hamming_loss(test_tags, preds_binarized)
# print("Hamming Loss: ", hamming_loss(test_tags, preds_binarized))


from sklearn.metrics import precision_recall_curve
Expand All @@ -170,7 +171,7 @@ def preprocess_image(img_path):
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], thresholds = precision_recall_curve(test_tags.ravel(), preds.ravel())
average_precision["micro"] = average_precision_score(test_tags, preds, average="micro")
print 'Average precision score, micro-averaged over all classes:', average_precision["micro"]
print('Average precision score, micro-averaged over all classes:', average_precision["micro"])

# Plot uAP v Recall curve
import matplotlib.pyplot as plt
Expand All @@ -186,4 +187,4 @@ def preprocess_image(img_path):
plt.title('Average precision score, micro-averaged over all classes: AUC={0:0.2f}'.format(average_precision["micro"]))
plt.savefig('PR_Curve.png')

print thresholds
print(thresholds)
11 changes: 6 additions & 5 deletions advanced_tag_models/simple_tag_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
import pickle
import numpy as np
from __future__ import print_function


video_folder = "../YouTube2Text/youtubeclips-dataset/"

Expand All @@ -23,7 +25,7 @@
parser.add_argument('-s', action='store', dest='model_size', type=int, nargs='+', help='Hidden State Sizes')
results = parser.parse_args()

print "Simple MLP Tag prediction network"
print("Simple MLP Tag prediction network")

tag_vectors = pickle.load(open("../"+results.tag_type+"_classifier/" + results.tag_type + "_vectors_long.pickle", "rb"))
# entity_vectors = pickle.load(open("../entity_classifier/entity_vectors_long.pickle", "rb"))
Expand All @@ -38,13 +40,13 @@
features = pickle.load(open("../frame_features/average_frame_features.pickle","rb"))
NUM_FEATURES = features[features.keys()[0]].shape[0]

print "NUM_FEATURES =", NUM_FEATURES
print("NUM_FEATURES =", NUM_FEATURES)

train_features = np.array([features.get(video,np.zeros(NUM_FEATURES)) for video in train_list])
test_features = np.array([features.get(video,np.zeros(NUM_FEATURES)) for video in test_list])


print "Features shape =", train_features.shape
print("Features shape =", train_features.shape)



Expand Down Expand Up @@ -109,7 +111,7 @@
# A "micro-average": quantifying score on all classes jointly
precision["micro"], recall["micro"], _ = precision_recall_curve(actual_tags.ravel(), pred_tags.ravel())
average_precision["micro"] = average_precision_score(actual_tags, pred_tags, average="micro")
print 'Average precision score, micro-averaged over all classes:', average_precision["micro"]
print('Average precision score, micro-averaged over all classes:', average_precision["micro"])

# Plot uAP v Recall curve
plt.switch_backend("agg")
Expand All @@ -123,4 +125,3 @@
plt.xlim([0.0, 1.0])
plt.title('Average precision score, micro-averaged over all classes: AUC={0:0.2f}'.format(average_precision["micro"]))
plt.savefig('PR_Curve_simple_model_regularized_'+results.tag_type+'.png')

Loading