l2_reg = 1e-3 learning_rate = 25 epochs = 5000 epoch0 = 10 epoch1 = 1000 target = 76 MEAN = np.float32([0.485, 0.456, 0.406]) STD = np.float32([0.229, 0.224, 0.225]) SAVE_PATH = "cs231n/datasets/squeezenet.ckpt" if not os.path.exists(SAVE_PATH + ".index"): raise ValueError("You need to download SqueezeNet!") model = SqueezeNet() model.load_weights(SAVE_PATH) model.trainable = False X = 255 * np.random.rand(224, 224, 3) X = process(X) X = X[None] X = tf.Variable(X) for _ in range(epochs): with tf.GradientTape() as tape: tape.watch(X) loss = model(X)[0, target] - l2_reg * tf.nn.l2_loss(X) dX = tape.gradient(loss, X) X.assign_add(dX[0] * learning_rate) X.assign(clip(X)) if _ % epoch0 == 0:
# for auto-reloading external modules # see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython #%load_ext autoreload #%autoreload 2 #%% Load the pretrained model. # this model is downloaded from here "http://cs231n.stanford.edu/squeezenet_tf2.zip" SAVE_PATH = 'cs231n/datasets/squeezenet.ckpt' if not os.path.exists(SAVE_PATH + ".index"): raise ValueError("You need to download SqueezeNet!") model = SqueezeNet() status = model.load_weights(SAVE_PATH) model.trainable = False #%% Load some ImageNet images from cs231n.data_utils import load_imagenet_val X_raw, y, class_names = load_imagenet_val(num=5) plt.figure(figsize=(12, 6)) for i in range(5): plt.subplot(1, 5, i + 1) plt.imshow(X_raw[i]) plt.title(class_names[y[i]]) plt.axis('off') plt.gcf().tight_layout()