-
-
Save PulkitS01/aa6e8e0ab04b1855620efe8bcef55943 to your computer and use it in GitHub Desktop.
# checking the accuracy of the predicted tags | |
from sklearn.metrics import accuracy_score | |
accuracy_score(predict, actual)*100 |
#defining the model architecture | |
model = Sequential() | |
model.add(Dense(1024, activation='relu', input_shape=(25088,))) | |
model.add(Dropout(0.5)) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(256, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(101, activation='softmax')) |
#defining the model architecture | |
model = Sequential() | |
model.add(Dense(1024, activation='relu', input_shape=(25088,))) | |
model.add(Dropout(0.5)) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(256, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(101, activation='softmax')) |
# creating the base model of pre-trained VGG16 model | |
base_model = VGG16(weights='imagenet', include_top=False) |
base_model = VGG16(weights='imagenet', include_top=False) |
# defining a function to save the weights of best model | |
from keras.callbacks import ModelCheckpoint | |
mcp_save = ModelCheckpoint('weight.hdf5', save_best_only=True, monitor='val_loss', mode='min') |
# compiling the model | |
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy']) |
import cv2 # for capturing videos | |
import math # for mathematical operations | |
import matplotlib.pyplot as plt # for plotting the images | |
%matplotlib inline | |
import pandas as pd | |
from keras.preprocessing import image # for preprocessing the images | |
import numpy as np # for mathematical operations | |
from keras.utils import np_utils | |
from skimage.transform import resize # for resizing images | |
from sklearn.model_selection import train_test_split | |
from glob import glob | |
from tqdm import tqdm |
import keras | |
from keras.models import Sequential | |
from keras.applications.vgg16 import VGG16 | |
from keras.layers import Dense, InputLayer, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D, GlobalMaxPooling2D | |
from keras.preprocessing import image | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from tqdm import tqdm | |
from sklearn.model_selection import train_test_split |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from keras.preprocessing import image | |
import numpy as np | |
import pandas as pd | |
from tqdm import tqdm | |
from keras.applications.vgg16 import VGG16 | |
import cv2 | |
import math | |
import os | |
from glob import glob | |
from scipy import stats as s |
# loading the trained weights | |
model.load_weights("weights.hdf5") |
# compiling the model | |
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy']) |
# normalizing the pixel values | |
max = X_train.max() | |
X_train = X_train/max | |
X_test = X_test/max |
# creating two lists to store predicted and actual tags | |
predict = [] | |
actual = [] | |
# for loop to extract frames from each test video | |
for i in tqdm(range(test_videos.shape[0])): | |
count = 0 | |
videoFile = test_videos[i] | |
cap = cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) # capturing the video from the given path | |
frameRate = cap.get(5) #frame rate | |
x=1 | |
# removing all other files from the temp folder | |
files = glob('temp/*') | |
for f in files: | |
os.remove(f) | |
while(cap.isOpened()): | |
frameId = cap.get(1) #current frame number | |
ret, frame = cap.read() | |
if (ret != True): | |
break | |
if (frameId % math.floor(frameRate) == 0): | |
# storing the frames of this particular video in temp folder | |
filename ='temp/' + "_frame%d.jpg" % count;count+=1 | |
cv2.imwrite(filename, frame) | |
cap.release() | |
# reading all the frames from temp folder | |
images = glob("temp/*.jpg") | |
prediction_images = [] | |
for i in range(len(images)): | |
img = image.load_img(images[i], target_size=(224,224,3)) | |
img = image.img_to_array(img) | |
img = img/255 | |
prediction_images.append(img) | |
# converting all the frames for a test video into numpy array | |
prediction_images = np.array(prediction_images) | |
# extracting features using pre-trained model | |
prediction_images = base_model.predict(prediction_images) | |
# converting features in one dimensional array | |
prediction_images = prediction_images.reshape(prediction_images.shape[0], 7*7*512) | |
# predicting tags for each array | |
prediction = model.predict_classes(prediction_images) | |
# appending the mode of predictions in predict list to assign the tag to the video | |
predict.append(y.columns.values[s.mode(prediction)[0][0]]) | |
# appending the actual tag of the video | |
actual.append(videoFile.split('/')[1].split('_')[1]) |
# creating an empty list | |
train_image = [] | |
# for loop to read and store frames | |
for i in tqdm(range(train.shape[0])): | |
# loading the image and keeping the target size as (224,224,3) | |
img = image.load_img('train_1/'+train['image'][i], target_size=(224,224,3)) | |
# converting it to array | |
img = image.img_to_array(img) | |
# normalizing the pixel value | |
img = img/255 | |
# appending the image to the train_image list | |
train_image.append(img) | |
# converting the list to numpy array | |
X = np.array(train_image) | |
# shape of the array | |
X.shape |
# reshaping the training as well as validation frames in single dimension | |
X_train = X_train.reshape(59075, 7*7*512) | |
X_test = X_test.reshape(14769, 7*7*512) |
# creating dummies of target variable for train and validation set | |
y_train = pd.get_dummies(y_train) | |
y_test = pd.get_dummies(y_test) |
# open the .txt file which have names of test videos | |
f = open("testlist01.txt", "r") | |
temp = f.read() | |
videos = temp.split('\n') | |
# creating a dataframe having video names | |
test = pd.DataFrame() | |
test['video_name'] = videos | |
test = test[:-1] | |
test.head() |
# getting the test list | |
f = open("testlist01.txt", "r") | |
temp = f.read() | |
videos = temp.split('\n') | |
# creating the dataframe | |
test = pd.DataFrame() | |
test['video_name'] = videos | |
test = test[:-1] | |
test_videos = test['video_name'] | |
test.head() |
# getting the names of all the images | |
images = glob("train_1/*.jpg") | |
train_image = [] | |
train_class = [] | |
for i in tqdm(range(len(images))): | |
# creating the image name | |
train_image.append(images[i].split('/')[1]) | |
# creating the class of image | |
train_class.append(images[i].split('/')[1].split('_')[1]) | |
# storing the images and their class in a dataframe | |
train_data = pd.DataFrame() | |
train_data['image'] = train_image | |
train_data['class'] = train_class | |
# converting the dataframe into csv file | |
train_data.to_csv('UCF/train_new.csv',header=True, index=False) |
# open the .txt file which have names of training videos | |
f = open("trainlist01.txt", "r") | |
temp = f.read() | |
videos = temp.split('\n') | |
# creating a dataframe having video names | |
train = pd.DataFrame() | |
train['video_name'] = videos | |
train = train[:-1] | |
train.head() |
# extracting features for training frames | |
X_train = base_model.predict(X_train) | |
X_train.shape |
train = pd.read_csv('UCF/train_new.csv') | |
train.head() |
# shape of images | |
X_train.shape |
# training the model | |
model.fit(X_train, y_train, epochs=200, validation_data=(X_test, y_test), callbacks=[mcp_save], batch_size=128) |
# storing the frames from training videos | |
for i in tqdm(range(train.shape[0])): | |
count = 0 | |
videoFile = train['video_name'][i] | |
cap = cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) # capturing the video from the given path | |
frameRate = cap.get(5) #frame rate | |
x=1 | |
while(cap.isOpened()): | |
frameId = cap.get(1) #current frame number | |
ret, frame = cap.read() | |
if (ret != True): | |
break | |
if (frameId % math.floor(frameRate) == 0): | |
# storing the frames in a new folder named train_1 | |
filename ='train_1/' + videoFile.split('/')[1].split(' ')[0] +"_frame%d.jpg" % count;count+=1 | |
cv2.imwrite(filename, frame) | |
cap.release() |
# extracting features for validation frames | |
X_test = base_model.predict(X_test) | |
X_test.shape |
# separating the target | |
y = train['class'] | |
# creating the training and validation set | |
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2, stratify = y) |
didn't store frame in csv , can you help me please
this code is not working
what is "UCF" in code "cv2.VideoCapture('UCF/'" ?
didn't store frame in csv
didn't store frame in csv
Hi @dipak5053,
I opened an issue a copule hours ago with the same issue of yours, but i deleted as i fixed the problem.
cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) this line of code is missing (the full path of the working PC, folder name and a slash), as in the UCF file there are several files. So, you must include the path of each folder, by adding another term of split which is (videoFile.split('/')[0]). Therefore, the final path will be, for instance:
cap = cv2.VideoCapture('C:/Users/PC_name/..../UCF/' + videoFile.split('/')[0] + '/' + videoFile.split('/')[1].split(' ')[0])
Note: The missing terms in the original code are in bold.
hope it helps you,
Thanks,
Hello! what is the data or file in that "UFC" folder?
i also can't work on training_tags.py.
It didn't store the frames in train_1 folder.
i am not able to store the frames in train_1 folder.. Suggest an idea for this
this code is not working what is "UCF" in code "cv2.VideoCapture('UCF/'" ?
It's called "Videos" earlier, the code needs some troubleshooting to get working. In-process myself.
i am not able to store the frames in train_1 folder.. Suggest an idea for this
You'll need to create the directory
do you have an enviorment.txt for this?