Created
August 22, 2017 21:48
-
-
Save hiwonjoon/d2d49b9a26344e925317ca501dd4e5f2 to your computer and use it in GitHub Desktop.
C3D Feature Extraction with https://github.com/axon-research/c3d-keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import better_exceptions | |
import os | |
import numpy as np | |
from tqdm import tqdm | |
from keras.models import model_from_json | |
import c3d_model | |
import sys | |
import keras.backend as K | |
dim_ordering = K.image_dim_ordering() | |
backend = dim_ordering | |
ROOT_DIR = 'DIR/TO/CONTAINING/VIDEO/FILES/' | |
EXTENSIONS = ('mp4','avi') | |
VIDEO_DIR = ROOT_DIR | |
FV_DIR = os.path.join(ROOT_DIR,'c3d_fv') | |
BATCH_SIZE = 64 | |
NUM_FRAMES = 16 | |
HEIGHT = 128 | |
WIDTH = 171 | |
FC_LAYER = 'fc6' #'fc7' | |
video_files = sorted([os.path.join(VIDEO_DIR,f) for f in os.listdir(VIDEO_DIR) if f.endswith(".avi")]) | |
# Helper functions. | |
import subprocess | |
def _read_video(filename,h,w) : | |
command = ['ffmpeg', | |
'-loglevel', 'error', | |
#'-ss', str(time), #from this time | |
'-i', filename, | |
#'-vframes', str(1), #get single frame | |
'-vf', 'scale=%d/%d'%(w,h), #given width and height | |
'-f', 'image2pipe', | |
'-pix_fmt', 'rgb24', | |
'-vcodec', 'rawvideo', '-'] | |
#print(command) | |
ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE ) | |
out, err = ffmpeg.communicate() | |
if(err) : print(err) | |
video = np.fromstring(out, dtype='uint8').reshape((-1,h,w,3)) | |
return video | |
def _aggregate_up_to(batch_size,video): | |
Xs = np.zeros([batch_size,NUM_FRAMES]+list(video.shape[1:])) | |
for i,s in enumerate(range(0,len(video)-NUM_FRAMES,NUM_FRAMES)): | |
Xs[i%batch_size] = video[s:s+NUM_FRAMES] | |
if( (i+1)%batch_size == 0 ): | |
yield Xs | |
if( (i+1)%batch_size != 0 ): | |
yield Xs[:i%batch_size+1] | |
# >>>>>>>>>>>>> Load Models... | |
model_weight_filename = os.path.join('./models', 'sports1M_weights_tf.h5') | |
model_json_filename = os.path.join('./models', 'sports1M_weights_tf.json') | |
model = model_from_json(open(model_json_filename, 'r').read()) | |
model.load_weights(model_weight_filename) | |
int_model = c3d_model.get_int_model(model=model, layer=FC_LAYER, backend=backend) | |
# subtract mean | |
mean_cube = np.load('models/train01_16_128_171_mean.npy') | |
mean_cube = np.transpose(mean_cube, (1, 2, 3, 0)) | |
# <<<<<<<<<<<< | |
# >>>>>>>>>>>> Extract Features! | |
for _,v in enumerate(tqdm(video_files)): | |
video = _read_video(v,HEIGHT,WIDTH) | |
fvs_name = os.path.join(FV_DIR,os.path.basename(os.path.splitext(v)[0]+'.npy')) | |
fvs = np.zeros((len(video)//NUM_FRAMES,4096)) | |
for i,(Xs) in enumerate(tqdm(_aggregate_up_to(BATCH_SIZE,video),total=len(video)//NUM_FRAMES//BATCH_SIZE)): | |
fv = int_model.predict_on_batch((Xs-mean_cube)[:,:,8:120,30:142,:]) # Crop Center | |
fvs[i*BATCH_SIZE:i*BATCH_SIZE+len(fv)] = fv | |
np.save(open(fvs_name,'w'),fvs) | |
# <<<<<<<<<<<< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from where can i get filles : sports1M_weights_tf.h5 , sports1M_weights_tf.json and train01_16_128_171_mean.npy.