Exemple #1
0
        x = self.relu(self.conv1_2(x))
        x = self.pool(x)

        # Apply block 2followed by max pool
        x = self.relu(self.conv2_1(x))
        x = self.relu(self.conv2_2(x))
        x = self.pool(x)
        # Prepare the image for the fully connected layer
        x = x.view(-1, int(64 * 32 / 2 / 2 * 32 / 2 / 2))

        # Apply the fully connected layer and return the result
        return self.fc(x)


img_shape = train_data.image_shape()
num_classes = train_data.num_classes()

net = Net(img_shape, num_classes)
clf = CnnClassifier(net, (0, *img_shape), num_classes, 0.01, 0.01)

not_improved_since = 0
best_accuracy = 0
stop_epoch = 0

for epoch in range(100):
    losses = []
    for batch in train_batches:
        loss = clf.train(batch.data, batch.label)
        losses.append(loss)
    losses = np.array(losses)
    mean = round(np.mean(losses), 3)
Exemple #2
0
    def forward(self, x):
        # run the steps ...
        return self.layers.forward(x)


# Learning rate to use
lr = 0.0001
# weight decay to use
wd = 0.0
# Step 4: wrap into CnnClassifier
net = CatsDogsModel()
# check whether GPU support is available
if torch.cuda.is_available():
    net.cuda()
clf = CnnClassifier(net, (0, 3, 32, 32), train_data.num_classes(), lr, wd)

netSimple = CatsDogsModelSimple()
# check whether GPU support is available
if torch.cuda.is_available():
    netSimple.cuda()
clfSimple = CnnClassifier(netSimple, (0, 3, 32, 32), train_data.num_classes(),
                          lr, wd)

netComplex = CatsDogsModelComplex()
# check whether GPU support is available
if torch.cuda.is_available():
    netComplex.cuda()
clfComplex = CnnClassifier(netComplex, (0, 3, 32, 32),
                           train_data.num_classes(), lr, wd)
# transfer to CUDA (if available)
if torch.cuda.is_available():
    pretrainedModel.cuda()
netTransfer = CatsDogsModelTransfer(pretrainedModel, 512)

# Learning rate to use
lr = 0.005
# weight decay to use
wd = 0.001

# Step 4: wrap into CnnClassifier
net = CatsDogsModel()
# check whether GPU support is available
if torch.cuda.is_available():
    net.cuda()
clf = CnnClassifier(net, (0, 3, 32, 32), train_data.num_classes(), lr, wd)

# check whether GPU support is available
if torch.cuda.is_available():
    netTransfer.cuda()
clfTransfer = CnnClassifier(
    netTransfer,
    (0, 3, 32, 32),
    train_data.num_classes(),
    lr,
    wd
)


# Step 5: train in 100 epochs
def train_model(clf: CnnClassifier, results_file: TextIO) -> TrainedModel:
Exemple #4
0
from dlvc.batches import BatchGenerator
from dlvc.test import Accuracy
from dlvc.dataset import Subset
import dlvc.ops as ops

np.random.seed(0)

pets_train = PetsDataset("../cifar-10-batches-py/", Subset.TRAINING)
pets_val = PetsDataset("../cifar-10-batches-py/", Subset.VALIDATION)

random_accuracy = Accuracy()
validation_accuracy = Accuracy()
train_accuracy = Accuracy()

print('Number of Classes = {}'.format(pets_train.num_classes()))
print('Number of Images = {}'.format(pets_train.__len__()))
print('First 10 Classes >>> {}'.format(pets_train.labels[:10]))

op = ops.chain([
    ops.vectorize(),
    ops.type_cast(np.float32),
    ops.add(-127.5),
    ops.mul(1 / 127.5),
])

train_batches = BatchGenerator(pets_train, 100, False, op)
validation_batches = BatchGenerator(pets_val, 100, False, op)

print('Number of Batches = {}'.format(train_batches.__len__()))