21
21
BATCH_SIZE = 128
22
22
MOMENTUM = 0.9
23
23
LR_DECAY = 0.0005
24
- LR_INIT = 0.1 # 0. 01
24
+ LR_INIT = 0.01
25
25
IMAGE_DIM = 227 # pixels
26
- NUM_CLASSES = 20 # 20 classes for VOC dataset - 1000 for original imagenet
26
+ NUM_CLASSES = 1000 # 20 classes for VOC dataset - 1000 for original imagenet
27
27
DEVICE_IDS = [0 , 1 ] # GPUs to use
28
28
# modify this to point to your data directory
29
29
INPUT_ROOT_DIR = 'alexnet_data_in'
30
- TRAIN_IMG_DIR = 'alexnet_data_in/voc-data-rearr '
30
+ TRAIN_IMG_DIR = 'alexnet_data_in/imagenet '
31
31
OUTPUT_DIR = 'alexnet_data_out/tblogs' # tensorboard logs
32
32
33
33
@@ -65,12 +65,12 @@ def __init__(self, num_classes=1000):
65
65
)
66
66
# classifier is just a name for linear layers
67
67
self .classifier = nn .Sequential (
68
+ nn .Dropout (p = 0.5 , inplace = True ),
68
69
nn .Linear (in_features = (256 * 6 * 6 ), out_features = 4096 ),
69
70
nn .ReLU (),
70
71
nn .Dropout (p = 0.5 , inplace = True ),
71
72
nn .Linear (in_features = 4096 , out_features = 4096 ),
72
73
nn .ReLU (),
73
- nn .Dropout (p = 0.5 , inplace = True ),
74
74
nn .Linear (in_features = 4096 , out_features = num_classes ),
75
75
)
76
76
@@ -105,17 +105,18 @@ def init_weights(m):
105
105
print ('AlexNet created' )
106
106
107
107
# create dataset and data loader
108
- imagenet_dataset = datasets .ImageFolder (TRAIN_IMG_DIR , transforms .Compose ([
108
+ dataset = datasets .ImageFolder (TRAIN_IMG_DIR , transforms .Compose ([
109
109
transforms .RandomResizedCrop (IMAGE_DIM ),
110
110
transforms .RandomHorizontalFlip (),
111
111
transforms .ToTensor (),
112
112
transforms .Normalize (mean = [0.485 , 0.456 , 0.406 ], std = [0.229 , 0.224 , 0.225 ]),
113
113
]))
114
114
print ('Dataset created' )
115
- imagenet_dataloader = data .DataLoader (
116
- imagenet_dataset ,
115
+ dataloader = data .DataLoader (
116
+ dataset ,
117
117
shuffle = True ,
118
118
pin_memory = True ,
119
+ drop_last = True ,
119
120
batch_size = BATCH_SIZE )
120
121
print ('Dataloader created' )
121
122
@@ -134,31 +135,36 @@ def init_weights(m):
134
135
tbwriter = SummaryWriter (log_dir = OUTPUT_DIR )
135
136
print ('TensorboardX summary writer created' )
136
137
138
+ # criterion defined
139
+ criterion = nn .CrossEntropyLoss ()
140
+ print ('Criterion defined' )
141
+
137
142
# start training!!
138
143
print ('Starting training...' )
139
144
total_steps = 1
140
145
for epoch in range (NUM_EPOCHS ):
141
146
lr_scheduler .step ()
142
- for imgs , classes in imagenet_dataloader :
147
+ for imgs , classes in dataloader :
143
148
imgs , classes = imgs .to (device ), classes .to (device )
144
149
optimizer .zero_grad ()
145
150
146
151
# calculate the loss
147
152
output = alexnet (imgs )
148
153
loss = F .cross_entropy (output , classes )
149
- # loss = F.nll_loss(F.log_softmax(output_logits, dim=1), target=classes)
150
-
151
- # log the information and add to tensorboard
152
- if total_steps % 10 == 0 :
153
- _ , preds = torch .max (output , 1 )
154
- accuracy = torch .sum (preds == classes ) / BATCH_SIZE
155
-
156
- print ('Epoch: {} \t Step: {} \t Loss: {:.4f} \t Acc: {:.4f}'
157
- .format (epoch + 1 , total_steps , loss .item (), accuracy .item ()))
158
- tbwriter .add_scalar ('loss' , loss .item (), total_steps )
154
+ # loss = F.nll_loss(F.log_softmax(output, dim=1), target=classes)
159
155
160
156
# update the parameters
161
157
loss .backward ()
162
158
optimizer .step ()
163
159
160
+ # log the information and add to tensorboard
161
+ if total_steps % 10 == 0 :
162
+ with torch .no_grad ():
163
+ _ , preds = torch .max (output , 1 )
164
+ accuracy = torch .sum (preds == classes )
165
+
166
+ print ('Epoch: {} \t Step: {} \t Loss: {:.4f} \t Acc: {}'
167
+ .format (epoch + 1 , total_steps , loss .item (), accuracy .item ()))
168
+ tbwriter .add_scalar ('loss' , loss .item (), total_steps )
169
+
164
170
total_steps += 1
0 commit comments