# Transfer Learning Transfer learning allows you to use a pre-trained feature extractor for your classifier. The idea of reusing learning from previously trained models on different but relative data sets is the basis of transfer learning. It allows you to train on a problem where there is a lot of data and to transfer learning to a problem with very little data. _Downstream Task_ - the task you are interested in solving ## Options in Transfer Learning 1. Freeze the weights: Layers closer to inputs learn more general features and those closer to the outputs are more specific to the problem. So just use the early features, freeze them for training and train on additional layers added to to the top. This method is effective when there is less data to train on. 2. Retrain entirely but start with previous weights: You still train entire mode but initialize with previous weights. This method is called *fine tuning* ```python weights_url = "https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5" weights_file = "inception_v3.h5" urllib.request.urlretrieve(weights_url, weights_file) # Instantiate the model pre_trained_model = InceptionV3(input_shape=(150, 150, 3), include_top=False, weights=None) # load pre-trained weights pre_trained_model.load_weights(weights_file) # freeze the layers for layer in pre_trained_model.layers: layer.trainable = False # pre_trained_model.summary() last_layer = pre_trained_model.get_layer('mixed7') print('last layer output shape: ', last_layer.output_shape) last_output = last_layer.output ``` ### Example Training CIFAR-10 with ResNET-50 ResNet 50 has been trained on ImageNET problem. ImageNET has images of size (224, 224, 3). CIFAR-10 has images of size (32, 32, 3). Inorder to use the CIFAR Images with RESNET you should add an upsample layer before the network. You can give a path to your own weights if you have it. ```python def feature_extractor(inputs): feature_extractor_layer = tf.keras.applications.resnet.ResNet50( input_shape=(224, 224, 3), include_top=False, weights='imagenet' )(inputs) return feaure_extractor_layer ``` ```python resize = tf.keras.layers.UpSampling2D(size=(7.7))(inputs) # This scales the image 7 times from 32 to 224 ```