-
Notifications
You must be signed in to change notification settings - Fork 0
/
task4_Exercise_Train_an_image_captioning_network_test3.py
85 lines (70 loc) · 3.57 KB
/
task4_Exercise_Train_an_image_captioning_network_test3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from utils.dataLoader import DataLoaderWrapper
from utils.saverRestorer import SaverRestorer
from utils.model import Model
from utils.trainer import Trainer
from utils.validate import plotImagesAndCaptions
from utils.validate_metrics import validateCaptions
from task4_cocoSource_xcnnfused import imageCaptionModel # here you plug in your modelfile depending on what you have developed: simple rnn, 2 layer, or attention, if you have 3 modelfiles a.py b.py c.py then you do: from a import ... or you have one file with n different imgcapmodels
def main(config, modelParam):
# create an instance of the model you want
model = Model(config, modelParam, imageCaptionModel)
# create an instacne of the saver and resoterer class
saveRestorer = SaverRestorer(config, modelParam)
if modelParam['inference'] == True:
model = saveRestorer.restore(model)
# create your data generator
dataLoader = DataLoaderWrapper(config, modelParam)
# here you train your model
if modelParam['inference'] == False:
# create trainer and pass all the previous components to it
trainer = Trainer(model, modelParam, config, dataLoader, saveRestorer)
trainer.train()
#plotImagesAndCaptions
if modelParam['inference'] == True:
#plotImagesAndCaptions(model, modelParam, config, dataLoader)
validateCaptions(model, modelParam, config, dataLoader)
return
########################################################################################################################
if __name__ == '__main__':
data_dir = '/itf-fi-ml/shared/IN5400/dataforall/mandatory2/data/coco/'#data/coco/'
#train
modelParam = {
'batch_size': 128, # Training batch size
'cuda': {'use_cuda': True, # Use_cuda=True: use GPU
'device_idx': 6}, # Select gpu index: 0,1,2,3
'numbOfCPUThreadsUsed': 10, # Number of cpu threads use in the dataloader
'numbOfEpochs': 60, # Number of epochs
'data_dir': data_dir, # data directory
'img_dir': 'loss_images_test/',
'modelsDir': 'storedModels_test/',
'modelName': 'model_4/', # name of your trained model
'restoreModelLast': 0,
'restoreModelBest': 0,
'modeSetups': [['train', True], ['val', True]],
'inNotebook': False, # If running script in jupyter notebook
'inference': False
}
config = {
'optimizer': 'adamW', # 'SGD' | 'adam' | 'RMSprop' | 'adamW'
'learningRate': {'lr': 0.001}, # learning rate to the optimizer
'weight_decay': 0.00001, # weight_decay value
'number_of_cnn_features': 2048, # Fixed, do not change
'embedding_size': 300, # word embedding size
'vocabulary_size': 10000, # number of different words
'truncated_backprop_length': 25,
'hidden_state_sizes': 512, #
'num_rnn_layers': 2, # number of stacked rnn's
'scheduler_milestones': [75,90], #45,70 end at 80? or 60, 80
'scheduler_factor': 0.2, #+0.25 dropout
#'featurepathstub': 'detectron2vg_features' ,
#'featurepathstub': 'detectron2m_features' ,
#'featurepathstub': 'detectron2cocov3_tenmfeatures' ,
'featurepathstub': 'detectron2_lim10features',
'cellType': 'LSTM' #'GRU' # RNN or GRU or LSTM??
}
if modelParam['inference'] == True:
modelParam['batch_size'] = 64
modelParam['modeSetups'] = [['val', False]]
modelParam['restoreModelBest'] = 1
main(config, modelParam)
aa = 1