<jupyter_start><jupyter_text>Training the REAL/FAKE classifier<jupyter_code>%reload_ext autoreload
%autoreload 2
from fastai.core import *
from fastai.vision import *
from fastai.callbacks import *
from efficientnet_pytorch import EfficientNet
import albumentations as A
from kgl_deepfake.data_aug import *<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>path = Path('../data/cropped_faces/')<jupyter_output><empty_output><jupyter_text>`SplitData` if using online selection model dfdcs 5, 10, 35, and 40 are used in online selection, so if you're using these models, you should exclude face crops from these dfdcs.<jupyter_code>_dfdcs, dfdcs = [5, 10, 35], [40,]
ant = pd.read_csv(path/'train.csv')
ant['dfdc'] = ant.directory.apply(lambda o: int(o.split('_')[-1]))
ovns = ant.set_index('dfdc').loc[_dfdcs + dfdcs].fname.unique()<jupyter_output><empty_output><jupyter_text>`ovns` are the names of the videos which should be excluded.<jupyter_code>train_items = get_image_files(path/'train')
len(train_items)
train_items = [o for o in train_items if o.name.split('_')[0] not in ovns]
len(train_items)
valid_items = get_image_files(path/'valid')
src = ImageList(train_items).split_none()
src.valid = ImageList(valid_items)<jupyter_output><empty_output><jupyter_text>`SplitData` if not using online selection model<jupyter_code>src = ImageList.from_folder(path).split_by_folder(train='train', valid='valid')<jupyter_output><empty_output><jupyter_text>Create `DataBunch` Copy these data augs from *09_DataAugmentation.ipynb*<jupyter_code>tfms = []
tfms += [alb_tfm2fastai(A.MotionBlur(blur_limit=9, p=.25), order=11)]
tfms += [alb_tfm2fastai(A.GaussNoise(var_limit=(15.0, 75.0), p=.25), order=12)]
tfms += [alb_tfm2fastai(A.JpegCompression(p=.25, quality_lower=30), order=13)]
tfms += [downscale(scale=0.5, p=.25)]
def get_data(bs,size):
    data = (src.label_from_re('([A-Z]+).jpg$')
           .transform(get_transforms(max_warp=0, max_zoom=1), size=size)
           .databunch(bs=bs).normalize(imagenet_stats))
    return data
bs, sz = 32, 256
data = get_data(bs, sz)
data.show_batch(rows=4, figsize=(10,7))<jupyter_output><empty_output><jupyter_text>Resnet<jupyter_code>model = models.resnet18(num_classes=2)
import copy
fc = copy.deepcopy(model.fc)
model.fc = nn.Identity()
state_dict = torch.load('models/resnet_oselect_dfdc.pth')
model.load_state_dict(state_dict['model'])
model.fc = fc
model.fc
learn = Learner(data, model, path='./', metrics=[accuracy])
learn.lr_find(start_lr=1e-11, end_lr=1e-7)
learn.recorder.plot(skip_end=5)
learn.fit_one_cycle(cyc_len=5, max_lr=1e-5)
learn.recorder.plot_losses()<jupyter_output><empty_output><jupyter_text>EfficientNetB0  Transfer from Imagenet<jupyter_code>model = EfficientNet.from_pretrained('efficientnet-b0', num_classes=data.train_dl.c)

learn = Learner(data, model, metrics=[accuracy])
#fm = flatten_model(model)
#learn.layer_groups = [nn.Sequential(*fm[:-3]), nn.Sequential(*fm[-3:])]
#learn.freeze_to(-1)
learn.lr_find()
learn.recorder.plot(skip_end=15)
learn.fit_one_cycle(cyc_len=20, max_lr=1e-3)
learn.save('effnetb0_classifier_ftimagenet')
learn.recorder.plot_losses()<jupyter_output><empty_output><jupyter_text>Transfer from Online Selection<jupyter_code>model = EfficientNet.from_pretrained('efficientnet-b0', num_classes=data.train_dl.c)

import copy
swish0 = copy.deepcopy(model._swish)
fc0 = copy.deepcopy(model._fc)
dropout0 = copy.deepcopy(model._dropout)
model._swish = nn.Identity()
model._fc = nn.Identity()
model._dropout = nn.Identity()<jupyter_output><empty_output><jupyter_text>Specify which oneline selection model to fine-tune.<jupyter_code>model_oselect = 'effnetb0_pairselect_margin1_39'
#state_dict = torch.load('models/effnetb0_oselect.pth')
state_dict = torch.load(f'models/pairselect_cycle0/{model_oselect}.pth')
model.load_state_dict(state_dict['model'])
model._swish = swish0
model._fc = fc0
model._dropout = dropout0
learn = Learner(data, model, metrics=[accuracy])
# Discriminative learning.  DON"T WORK
#fm = flatten_model(model)
#learn.layer_groups = [nn.Sequential(*fm[:-3]), nn.Sequential(*fm[-3:])]
#learn.freeze_to(-1)
learn.load('ft_effnetb0_pairselect_margin1_39');
learn.lr_find()
learn.recorder.plot(skip_end=0)
callbacks = [SaveModelCallback(learn, monitor='valid_loss', 
                               every='improvement', 
                               name=f'ft_{model_oselect}')]
learn.fit_one_cycle(cyc_len=5, max_lr=1e-5)
learn.recorder.plot_losses()
learn.recorder.plot_losses()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14503648021224852, 'fraction_numerical': 0.023435772717223084, 'mean_word_length': 7.255474452554744, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '472733', 'license': 'Apache-2.0', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1727, 'n_words': 261, 'path': 'nbs/classifier_training.ipynb', 'repo_name': 'qAp/kgl_deepfake', 'size': '4523'}