<jupyter_start><jupyter_text>Load Images<jupyter_code>reload(trainModel)

def createDataloaders(data_dir, input_size=224): 
    xs = ['train', 'val']

    data_transforms = trainModel.create_data_transforms(input_size, input_size)
    
    image_datasets = {x: p(data_dir, 
                           lambda _:os.path.join(_, x),
                           lambda _: datasets.ImageFolder(_, data_transforms[x])
                          )
                      for x in xs}

    dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=4,
                                                 shuffle=True, num_workers = 4)
                   for x in xs}

    dataset_sizes = {x: len(image_datasets[x]) for x in xs}
    
    return image_datasets, dataloaders, dataset_sizes
data_dir = 'scrap_data400/'
image_datasets, dataloaders, dataset_sizes = createDataloaders(data_dir)
class_names = image_datasets['train'].classes
class_names
n_classes = len(class_names)
dataset_sizes<jupyter_output><empty_output><jupyter_text>Visualize<jupyter_code>reload(visualizeModel)

inputs, classes = next(iter(dataloaders['train']))

out = torchvision.utils.make_grid(inputs)

visualizeModel.imshow(out, mn, sd, 
                      title=[class_names[x] for x in classes])<jupyter_output><empty_output><jupyter_text>Finetune Convnet<jupyter_code>torch.cuda.get_device_name(0)
model
reload(trainModel)
trainModel.viewParamsToBeUpdated(model)
reload(trainModel)

model = trainModel.train_model(
    model, criterion, optimizer, exp_lr_scheduler, dataloaders, 
    dataset_sizes,
    device, num_epochs=25)<jupyter_output><empty_output><jupyter_text>Visualize Results<jupyter_code>reload(trainModel)
trainModel.visualize_model(model, dataloaders, device, class_names, 
                           trainModel.defaultMn, 
                           trainModel.defaultSd, k='val')<jupyter_output><empty_output><jupyter_text>Hyper Parameter Search<jupyter_code>def makeHyperParamName(heir_array):
    '/'.join(heir_array)
trainModel.viewParamsToBeUpdated(model)
reload(trainModel)

def trainWithParams(runDir, fine_tune_params_filter = [],  
                    model_type="resnet", 
                    dataloaders = dataloaders, dataset_sizes = dataset_sizes,
                    n_epochs = 25):
    
    model = trainModel.initialize_model(model_type, n_classes,
                                               True)
    model = model.to(device)
    

    p(
        trainModel.findParam(model, name_filter = fine_tune_params_filter),
        lambda named_params: [param_value for (_, param_value) in named_params],
        lambda pvs: trainModel.setParameterRequiresGrad(
            model, params = pvs, requires_grad = True)
    )
        
    writer = p('runs/' + runDir, SummaryWriter)
    
    criterion = nn.CrossEntropyLoss()

    # Observe that all parameters are being optimized
    optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9)

    # Decay LR by a factor of 0.1 every 7 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)

    model = trainModel.train_model(
        model, criterion, optimizer, exp_lr_scheduler, 
        dataloaders, dataset_sizes, device, 
        num_epochs=n_epochs, writer=writer)
    
    return model
model
train_ns = [400, 1000, 1500]
tune_params = [
    ['fc'],
    ['layer4.1.bn2'],
    ['layer4.1', '2'],
    ['layer4.1'],
    ['layer4']
]
[pn for (pn, _) in trainModel.findParam(model, tune_params[0])]
['_'.join(tp) for tp in tune_params]
model_type = 'resnet'
models = dict()
for tp in tune_params:
    for train_n in train_ns:
        runDir = '/'.join(['resnet', '_'.join(tp), str(train_n)])
        print(runDir)
        
        _, dataloaders, dataset_sizes = createDataloaders('scrap_data' + str(train_n))
        
        model = trainWithParams(runDir, fine_tune_params_filter = tp,  
                                model_type=model_type, 
                                dataloaders = dataloaders, dataset_sizes = dataset_sizes,
                               n_epochs=25)
        models[runDir] = model
models
for (k, m) in models.items():
    print(k)
    print(trainModel.viewParamsToBeUpdated(m))
    
m = trainModel.initialize_model("alexnet", n_classes, True)
m<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10351244475459409, 'fraction_numerical': 0.010700162828564782, 'mean_word_length': 2.8914027149321266, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '348301', 'license': 'MIT', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1399, 'n_words': 248, 'path': 'Transfer.ipynb', 'repo_name': 'JDMusc/Online-Bullying-Image-Classifcation', 'size': '4299'}