Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<define_variables>
warnings.filterwarnings('ignore')
Image('.. /input/deepfake-kernel-data/google_cloud_vm.png' )<define_variables>
training_folder = '.. /input/cassava-leaf-disease-classification/train_images/'
Image('.. /input/deepfake-kernel-data/lr_15e-2_epochs_42_patience_5.png' )<define_variables>
img = Image.open(".. /input/cassava-leaf-disease-classification/train_images/1277648239.jpg")
 plt.imshow(img)
 plt.show()
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_10_patience_5.png' )<define_variables>
samples_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv")
 samples_df = shuffle(samples_df, random_state=42)
 samples_df["filepath"] = training_folder+samples_df["image_id"]
samples_df[:10]
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_20_patience_5.png' )<define_variables>
y=samples_df['label'].values
y = to_categorical(y )
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_12_patience_2.png' )<define_variables>
batch_size = 8
image_size = 512
input_shape =(image_size, image_size, 3)
 dropout_rate = 0.4
classes_to_predict = sorted(samples_df.label.unique() )
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_30_patience_2.png' )<define_variables>
X_train, X_test, y_train, y_test = train_test_split(samples_df, y, random_state=42, test_size=0.2 )
Image('.. /input/deepfake-kernel-data/google_cloud_vm_deepfake_training_screenshot.png' )<set_options>
training_data = tf.data.Dataset.from_tensor_slices(( X_train.filepath.values, y_train))
validation_data = tf.data.Dataset.from_tensor_slices(( X_test.filepath.values, y_test))
%matplotlib inline
warnings.filterwarnings("ignore" )<define_variables>
def load_image_and_label_from_path(image_path, label):
img = tf.io.read_file(image_path)
 img = tf.image.decode_jpeg(img, channels=3)
 return img,label
AUTOTUNE = tf.data.experimental.AUTOTUNE
training_data = training_data.map(load_image_and_label_from_path, num_parallel_calls=AUTOTUNE)
 validation_data = validation_data.map(load_image_and_label_from_path, num_parallel_calls=AUTOTUNE )
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos )<import_modules>
training_data_batches = training_data.shuffle(buffer_size=1000 ).batch(batch_size ).prefetch(buffer_size=AUTOTUNE)
 validation_data_batches = validation_data.shuffle(buffer_size=1000 ).batch(batch_size ).prefetch(buffer_size=AUTOTUNE )
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
adapt_data = tf.data.Dataset.from_tensor_slices(X_train.filepath.values)
 def adapt_mode(image_path):
img = tf.io.read_file(image_path)
 img = tf.image.decode_jpeg(img, channels=3)
 img = layers.experimental.preprocessing.Rescaling(1.0 / 255 )(img)
 return img
adapt_data = adapt_data.map(adapt_mode, num_parallel_calls=AUTOTUNE)
 adapt_data_batches = adapt_data.shuffle(buffer_size=1000 ).batch(batch_size ).prefetch(buffer_size=AUTOTUNE )
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
data_augmentation_layers = tf.keras.Sequential(
 [
layers.experimental.preprocessing.RandomCrop(height=image_size, width=image_size),
layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
layers.experimental.preprocessing.RandomRotation(0.25),
layers.experimental.preprocessing.RandomZoom(( -0.2, 0)) ,
layers.experimental.preprocessing.RandomContrast(( 0.2,0.2)) ,
brighten() ,
saturate() ,
]
)
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
image = Image.open(".. /input/cassava-leaf-disease-classification/train_images/1481899695.jpg")
 plt.imshow(image)
 plt.show()
frames_per_video = 64
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
image = tf.expand_dims(np.array(image), 0 )
input_size = 224<normalization>
plt.figure(figsize=(12, 12))
for i in range(16):
augmented_image = data_augmentation_layers(image)
 ax = plt.subplot(4, 4, i + 1)
 plt.imshow(augmented_image[0])
 plt.axis("off" )
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
def create_model() :
efficientnet= EfficientNetB4(weights=".. /input/tfkeras-efficientnet-weights/efficientnetb4_notop.h5",
include_top=False,
input_shape=input_shape, 
)
 input_layer = Input(shape = input_shape)
 augmented = data_augmentation_layers(input_layer)
 efficientnet = efficientnet(augmented)
 pooling = layers.GlobalAveragePooling2D()(efficientnet)
 dropout = layers.Dropout(dropout_rate )(pooling)
 outputs = Dense(len(classes_to_predict), activation="softmax" )(dropout)
 model = Model(inputs=input_layer, outputs=outputs)
 return model
model = create_model()
model.summary()
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )<load_pretrained>
%%time
model.get_layer('efficientnetb4' ).get_layer('normalization' ).adapt(adapt_data_batches )
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
def log_t(u, t):
epsilon = 1e-7

 if t == 1.0:
return tf.math.log(u + epsilon)
 else:
return(u**(1.0 - t)- 1.0)/(1.0 - t)
 def bi_tempered_logistic_loss(y_pred, y_true, t1, label_smoothing=0.0):

 y_pred = tf.cast(y_pred, tf.float32)
 y_true = tf.cast(y_true, tf.float32)
 if label_smoothing > 0.0:
num_classes = tf.cast(tf.shape(y_true)[-1], tf.float32)
 y_true =(1 - num_classes /(num_classes - 1)* label_smoothing)* y_true + label_smoothing /(num_classes - 1)
 temp1 =(log_t(y_true + 1e-7, t1)- log_t(y_pred, t1)) * y_true
temp2 =(1 /(2 - t1)) *(tf.math.pow(y_true, 2 - t1)- tf.math.pow(y_pred, 2 - t1))
loss_values = temp1 - temp2
return tf.math.reduce_sum(loss_values, -1)
 class BiTemperedLogisticLoss(tf.keras.losses.Loss):
def __init__(self, t1=0.8, label_smoothing=0.2):
super(BiTemperedLogisticLoss, self ).__init__()
self.t1 = t1
self.label_smoothing = label_smoothing
def call(self, y_true, y_pred):
return bi_tempered_logistic_loss(y_pred, y_true, self.t1, self.label_smoothing )
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
epochs = 8
decay_steps = int(round(len(X_train)/batch_size)) *epochs
cosine_decay = CosineDecay(initial_learning_rate=1e-5, decay_steps=decay_steps, alpha=0.3)
 callbacks = [ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
loss = BiTemperedLogisticLoss()
model.compile(loss=loss, optimizer=tf.keras.optimizers.Adam(cosine_decay), metrics=["accuracy"] )
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
history = model.fit(training_data_batches,
epochs = epochs,
validation_data = validation_data_batches,
callbacks = callbacks )
speed_test = False<predict_on_test>
model.load_weights("best_model.h5" )
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
def run_predictions_over_image_list(image_list, folder):
predictions = []
with tqdm(total=len(image_list)) as pbar:
for image_filename in image_list:
pbar.update(1)
 predictions.append(predict_and_vote(image_filename, folder))
return predictions
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
X_test["results"] = run_predictions_over_image_list(X_test["image_id"], training_folder )
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )<install_modules>
true_positives = 0
prediction_distribution_per_class = {"0":{"0": 0, "1": 0, "2":0, "3":0, "4":0},
"1":{"0": 0, "1": 0, "2":0, "3":0, "4":0},
"2":{"0": 0, "1": 0, "2":0, "3":0, "4":0},
"3":{"0": 0, "1": 0, "2":0, "3":0, "4":0},
"4":{"0": 0, "1": 0, "2":0, "3":0, "4":0}}
number_of_images = len(X_test)
 for idx, pred in X_test.iterrows() :
if int(pred["label"])== pred.results:
true_positives+=1
prediction_distribution_per_class[str(pred["label"])][str(pred.results)] += 1
print("accuracy: {}%".format(true_positives/number_of_images*100))
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet<define_variables>
test_folder = '.. /input/cassava-leaf-disease-classification/test_images/'
submission_df = pd.DataFrame(columns={"image_id","label"})
 submission_df["image_id"] = os.listdir(test_folder)
 submission_df["label"] = 0
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )<set_options>
submission_df["label"] = run_predictions_over_image_list(submission_df["image_id"], test_folder )
<load_pretrained><EOS> 
submission_df.to_csv("submission.csv", index=False )