Extracting the image features

Here, we combine all the logic that we have seen individually for the algorithms in the chapter: 

### For ResNet

trn_labels = []
trn_resnet_features = []
for d,la in train_loader:
o = my_resnet(Variable(d.cuda()))
o = o.view(o.size(0),-1)
trn_labels.extend(la)
trn_resnet_features.extend(o.cpu().data)
val_labels = []
val_resnet_features = []
for d,la in val_loader:
o = my_resnet(Variable(d.cuda()))
o = o.view(o.size(0),-1)
val_labels.extend(la)
val_resnet_features.extend(o.cpu().data)

### For Inception

trn_inception_features = LayerActivations(my_inception.Mixed_7c)
for da,la in train_loader:
_ = my_inception(Variable(da.cuda()))

trn_inception_features.remove()

val_inception_features = LayerActivations(my_inception.Mixed_7c)
for da,la in val_loader:
_ = my_inception(Variable(da.cuda()))

val_inception_features.remove()

### For DenseNet


trn_densenet_features = []
for d,la in train_loader:
o = my_densenet(Variable(d.cuda()))
o = o.view(o.size(0),-1)

trn_densenet_features.extend(o.cpu().data)


val_densenet_features = []
for d,la in val_loader:
o = my_densenet(Variable(d.cuda()))
o = o.view(o.size(0),-1)
val_densenet_features.extend(o.cpu().data)

By now, we have created image features using all the models. If you are facing memory issues, then you can either remove one of the models, or stop storing the features in the memory, which could be slow to train. If you are running this on a CUDA instance, then you can go for a more powerful instance. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.127.37