Skip to content

Commit 4e49af1

Browse files
committed
Model using ImageNet
1 parent 267ff05 commit 4e49af1

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

extract_imagenet.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
mkdir imagenet
4+
5+
tar -xf ILSVRC2012_img_train.tar -C imagenet
6+
7+
cd imagenet
8+
9+
extract_to_folder() {
10+
BASENAME=$(basename $1) # strip the path
11+
echo $BASENAME
12+
FNAME=${BASENAME%.*} # extract filename without extension
13+
mkdir $FNAME
14+
tar -xf $1 -C $FNAME
15+
rm $1 # remove the tar after unzipping
16+
}
17+
18+
export -f extract_to_folder
19+
find . -name "n*.tar" -exec bash -c 'extract_to_folder {}' \;

model.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
BATCH_SIZE = 128
2222
MOMENTUM = 0.9
2323
LR_DECAY = 0.0005
24-
LR_INIT = 0.1 # 0.01
24+
LR_INIT = 0.01
2525
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
2727
DEVICE_IDS = [0, 1] # GPUs to use
2828
# modify this to point to your data directory
2929
INPUT_ROOT_DIR = 'alexnet_data_in'
30-
TRAIN_IMG_DIR = 'alexnet_data_in/voc-data-rearr'
30+
TRAIN_IMG_DIR = 'alexnet_data_in/imagenet'
3131
OUTPUT_DIR = 'alexnet_data_out/tblogs' # tensorboard logs
3232

3333

@@ -65,12 +65,12 @@ def __init__(self, num_classes=1000):
6565
)
6666
# classifier is just a name for linear layers
6767
self.classifier = nn.Sequential(
68+
nn.Dropout(p=0.5, inplace=True),
6869
nn.Linear(in_features=(256 * 6 * 6), out_features=4096),
6970
nn.ReLU(),
7071
nn.Dropout(p=0.5, inplace=True),
7172
nn.Linear(in_features=4096, out_features=4096),
7273
nn.ReLU(),
73-
nn.Dropout(p=0.5, inplace=True),
7474
nn.Linear(in_features=4096, out_features=num_classes),
7575
)
7676

@@ -105,17 +105,18 @@ def init_weights(m):
105105
print('AlexNet created')
106106

107107
# create dataset and data loader
108-
imagenet_dataset = datasets.ImageFolder(TRAIN_IMG_DIR, transforms.Compose([
108+
dataset = datasets.ImageFolder(TRAIN_IMG_DIR, transforms.Compose([
109109
transforms.RandomResizedCrop(IMAGE_DIM),
110110
transforms.RandomHorizontalFlip(),
111111
transforms.ToTensor(),
112112
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
113113
]))
114114
print('Dataset created')
115-
imagenet_dataloader = data.DataLoader(
116-
imagenet_dataset,
115+
dataloader = data.DataLoader(
116+
dataset,
117117
shuffle=True,
118118
pin_memory=True,
119+
drop_last=True,
119120
batch_size=BATCH_SIZE)
120121
print('Dataloader created')
121122

@@ -134,31 +135,36 @@ def init_weights(m):
134135
tbwriter = SummaryWriter(log_dir=OUTPUT_DIR)
135136
print('TensorboardX summary writer created')
136137

138+
# criterion defined
139+
criterion = nn.CrossEntropyLoss()
140+
print('Criterion defined')
141+
137142
# start training!!
138143
print('Starting training...')
139144
total_steps = 1
140145
for epoch in range(NUM_EPOCHS):
141146
lr_scheduler.step()
142-
for imgs, classes in imagenet_dataloader:
147+
for imgs, classes in dataloader:
143148
imgs, classes = imgs.to(device), classes.to(device)
144149
optimizer.zero_grad()
145150

146151
# calculate the loss
147152
output = alexnet(imgs)
148153
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: {} \tStep: {} \tLoss: {:.4f} \tAcc: {:.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)
159155

160156
# update the parameters
161157
loss.backward()
162158
optimizer.step()
163159

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: {} \tStep: {} \tLoss: {:.4f} \tAcc: {}'
167+
.format(epoch + 1, total_steps, loss.item(), accuracy.item()))
168+
tbwriter.add_scalar('loss', loss.item(), total_steps)
169+
164170
total_steps += 1

0 commit comments

Comments
 (0)