Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
!nvidia-smi
params = { 'n_estimators' : [1500, 2000, 2500],
'learning_rate' : [0.01, 0.02]
}
xgb = XGBRegressor(
 objective = 'reg:squarederror',
subsample = 0.8,
colsample_bytree = 0.8,
learning_rate = 0.01,
tree_method = 'gpu_hist')
 grid_search = GridSearchCV(xgb,
param_grid = params,
scoring = 'neg_root_mean_squared_error',
n_jobs = -1,
verbose = 10)
 grid_search.fit(train_data, y_train)
 print('
Best estimator:')
 print(grid_search.best_estimator_)
 print('
Best score:')
 print(grid_search.best_score_)
 print('
Best hyperparameters:')
 print(grid_search.best_params_ )<train_model>
%matplotlib inline
sns.set(style='white', context='notebook', palette='deep')
 np.random.seed(2 )
clf = XGBRegressor(
 objective = 'reg:squarederror',
subsample = 0.8,
learning_rate = 0.02,
max_depth = 7,
n_estimators = 2500,
tree_method = 'gpu_hist')
 clf.fit(train_data, y_train)
 y_pred_xgb = clf.predict(test_data)
 print(y_pred_xgb )<save_to_csv>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
solution = pd.DataFrame({"id":test_data.id, "target":y_pred_xgb})
 solution.to_csv("solution.csv", index = False)
 print("saved successful!" )<install_modules>
y_train = train["label"]
X_train = train.drop(labels=["label"], axis = 1 )
!pip install.. /input/efficientnet/efficientnet-1.0.0-py3-none-any.whl<import_modules>
( X_train1, y_train1),(X_test1, y_test1)= mnist.load_data()
X_train1 = np.concatenate([X_train1, X_test1], axis=0)
 y_train1 = np.concatenate([y_train1, y_test1], axis=0)
 X_train1 = X_train1.reshape(-1, 28*28 )
import pandas as pd
import tensorflow as tf
import cv2
import glob
from tqdm.notebook import tqdm
import numpy as np
import os
import efficientnet.keras as efn
from keras.layers import *
from keras import Model
import matplotlib.pyplot as plt
import time<load_pretrained>
X_train = X_train/255.
 X_train1 = X_train1/255.
 test = test/255 .
detection_graph = tf.Graph()
with detection_graph.as_default() :
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile('.. /input/mobilenet-face/frozen_inference_graph_face.pb', 'rb')as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
 tf.import_graph_def(od_graph_def, name='' )<set_options>
X_train = np.concatenate(( X_train.values, X_train1))
y_train = np.concatenate(( y_train, y_train1))
cm = detection_graph.as_default()
cm.__enter__()<prepare_x_and_y>
y_train = to_categorical(y_train, num_classes = 10 )
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess=tf.compat.v1.Session(graph=detection_graph, config=config)
 image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
 boxes_tensor = detection_graph.get_tensor_by_name('detection_boxes:0')
 scores_tensor = detection_graph.get_tensor_by_name('detection_scores:0')
 num_detections = detection_graph.get_tensor_by_name('num_detections:0' )<categorify>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 2 )
def get_img(images):
global boxes,scores,num_detections
im_heights,im_widths=[],[]
imgs=[]
for image in images: 
(im_height,im_width)=image.shape[:-1]
imgs.append(image)
 im_heights.append(im_height)
 im_widths.append(im_widths)
 imgs=np.array(imgs)
(boxes, scores_)= sess.run(
 [boxes_tensor, scores_tensor],
feed_dict={image_tensor: imgs})
 finals=[]
for x in range(boxes.shape[0]):
scores=scores_[x]
max_=np.where(scores==scores.max())[0][0]
box=boxes[x][max_]
ymin, xmin, ymax, xmax = box 
(left, right, top, bottom)=(xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
 left, right, top, bottom = int(left), int(right), int(top), int(bottom)
 image=imgs[x]
finals.append(cv2.cvtColor(cv2.resize(image[max([0,top-40]):bottom+80,max([0,left-40]):right+80],(240,240)) ,cv2.COLOR_BGR2RGB))
return finals
def detect_video(video, start_frame):
frame_count=10
capture = cv2.VideoCapture(video)
 v_len = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idxs = np.linspace(start_frame,v_len,frame_count, endpoint=False, dtype=np.int)
 imgs=[]
i=0
for frame_idx in range(int(v_len)) :
ret = capture.grab()
if not ret:
print("Error grabbing frame %d from movie %s" %(frame_idx, video))
if frame_idx >= frame_idxs[i]:
if frame_idx-frame_idxs[i]>20:
return None
ret, frame = capture.retrieve()
if not ret or frame is None:
print("Error retrieving frame %d from movie %s" %(frame_idx, video))
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 imgs.append(frame)
 i += 1
if i >= len(frame_idxs):
break
imgs=get_img(imgs)
 if len(imgs)<10:
return None
return np.hstack(imgs)
<train_model>
print(f"Training shape {X_train.shape}
Validation shape {X_val.shape}" )
os.mkdir('./videos/')
 for x in tqdm(glob.glob('.. /input/deepfake-detection-challenge/test_videos/*.mp4')) :
try:
filename=x.replace('.. /input/deepfake-detection-challenge/test_videos/','' ).replace('.mp4','.jpg')
 a=detect_video(x,0)
 if a is None:
continue
cv2.imwrite('./videos/'+filename,a)
 except Exception as err:
print(err )<train_model>
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))
os.mkdir('./videos_2/')
 for x in tqdm(glob.glob('.. /input/deepfake-detection-challenge/test_videos/*.mp4')) :
try:
filename=x.replace('.. /input/deepfake-detection-challenge/test_videos/','' ).replace('.mp4','.jpg')
 a=detect_video(x,95)
 if a is None:
continue
cv2.imwrite('./videos_2/'+filename,a)
 except Exception as err:
print(err )<choose_model_class>
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image('model.png' )
bottleneck = efn.EfficientNetB1(weights=None,include_top=False,pooling='avg')
 inp=Input(( 10,240,240,3))
x=TimeDistributed(bottleneck )(inp)
 x = LSTM(128 )(x)
 x = Dense(64, activation='elu' )(x)
 x = Dense(1,activation='sigmoid' )(x )<define_variables>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] )
model=Model(inp,x)
 weights = ['.. /input/deepfake-20/saved-model-01-0.06.hdf5', '.. /input/deepfake-20/saved-model-02-0.05.hdf5', '.. /input/model-epoch-3/saved-model-03-0.06.hdf5','.. /input/model-02/saved-model-01-0.06.hdf5']*2
sub_file = ['submission_'+str(i)+'.csv' for i in range(1,9)]
video = ['./videos/']*4+['./videos_2/']*4
for xxxxx in range(8):
start = time.time()
model.load_weights(weights[xxxxx])
 def get_birghtness(img):
return img/img.max()
def process_img(img,flip=[False]*10):
imgs=[]
for x in range(10):
if flip[x]:
imgs.append(get_birghtness(cv2.flip(img[:,x*240:(x+1)*240,:],1)))
 else:
imgs.append(get_birghtness(img[:,x*240:(x+1)*240,:]))
return np.array(imgs)
 sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 test_files=glob.glob(video[xxxxx]+'*.jpg')
 submission=pd.DataFrame()
submission['filename']=os.listdir(( '.. /input/deepfake-detection-challenge/test_videos/'))
submission['label']=0.5
filenames=[]
batch=[]
batch1=[]
batch2=[]
batch3=[]
preds=[]
for x in test_files:
img=process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB))
if img is None:
continue
batch.append(img)
 batch1.append(process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB),[True]*10))
batch2.append(process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB),[True,False]*5))
batch3.append(process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB),[False,True]*5))
filenames.append(x.replace(video[xxxxx],'' ).replace('.jpg','.mp4'))
if len(batch)==16:
preds+=(((0.25*model.predict(np.array(batch)))) +(( 0.25*model.predict(np.array(batch1)))) +(( 0.25*model.predict(np.array(batch2)))) +(( 0.25*model.predict(np.array(batch3)))) ).tolist()
batch=[]
batch1=[]
batch2=[]
batch3=[]
if len(batch)!=0:
preds+=(((0.25*model.predict(np.array(batch)))) +(( 0.25*model.predict(np.array(batch1)))) +(( 0.25*model.predict(np.array(batch2)))) +(( 0.25*model.predict(np.array(batch3)))) ).tolist()
print(time.time() -start)
 new_preds=[]
for x in preds:
new_preds.append(x[0])
 print(sum(new_preds)/len(new_preds))
for x,y in zip(new_preds,filenames):
submission.loc[submission['filename']==y,'label']=min([max([0.05,x]),0.95])
 submission.to_csv(sub_file[xxxxx], index=False )<set_options>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.2,
min_lr=0.00001)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=15)
 checkpoint = ModelCheckpoint(filepath='model.h5', monitor='val_loss', mode='min', save_best_only=True, save_weights_only=True )
!rm -r videos
!rm -r videos_2<load_from_csv>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
df1 = pd.read_csv('submission_1.csv' ).set_index('filename' ).transpose().to_dict()
df2 = pd.read_csv('submission_2.csv' ).set_index('filename' ).transpose().to_dict()
df3 = pd.read_csv('submission_3.csv' ).set_index('filename' ).transpose().to_dict()
df4 = pd.read_csv('submission_4.csv' ).set_index('filename' ).transpose().to_dict()
df5 = pd.read_csv('submission_5.csv' ).set_index('filename' ).transpose().to_dict()
df6 = pd.read_csv('submission_6.csv' ).set_index('filename' ).transpose().to_dict()
df7 = pd.read_csv('submission_7.csv' ).set_index('filename' ).transpose().to_dict()
df8 = pd.read_csv('submission_8.csv' ).set_index('filename' ).transpose().to_dict()
filename = []
label = []
for i in df1.keys() :
filename.append(i)
 a = []
if df1[i]['label']!=0.5:
a.append(df1[i]['label'])
 if df2[i]['label']!=0.5:
a.append(df2[i]['label'])
 if df3[i]['label']!=0.5:
a.append(df3[i]['label'])
 if df4[i]['label']!=0.5:
a.append(df4[i]['label'])
 if df5[i]['label']!=0.5:
a.append(df5[i]['label'])
 if df6[i]['label']!=0.5:
a.append(df6[i]['label'])
 if df7[i]['label']!=0.5:
a.append(df7[i]['label'])
 if df8[i]['label']!=0.5:
a.append(df8[i]['label'])
 if len(a)==0:
label.append(0.5)
 else:
label.append(min([max([0.05,sum(a)/len(a)]),0.95]))
df = pd.DataFrame()
df['filename'] = filename
df['label'] = label
print(np.array(df['label'] ).mean())
 df.to_csv('submission.csv', index=False )<load_from_csv>
epochs = 50
batch_size = 128
!rm submission_1.csv
!rm submission_2.csv
!rm submission_3.csv
!rm submission_4.csv
!rm submission_5.csv
!rm submission_6.csv
!rm submission_7.csv
!rm submission_8.csv<set_options>
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=epochs,
validation_data=(X_val, y_val),
verbose=2,
steps_per_epoch=X_train.shape[0]//batch_size,
callbacks=[learning_rate_reduction, es, checkpoint] )
%matplotlib inline
warnings.filterwarnings('ignore')
 pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_columns', 200 )<load_from_csv>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<count_missing_values><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )