Skip to content

Instantly share code, notes, and snippets.

@theavicaster
Created May 17, 2021 06:16
Show Gist options
  • Save theavicaster/e261acaed1e7729ab31e39d7c4f42247 to your computer and use it in GitHub Desktop.
Save theavicaster/e261acaed1e7729ab31e39d7c4f42247 to your computer and use it in GitHub Desktop.
SF40Attention.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"accelerator": "GPU",
"colab": {
"name": "SF40Attention.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/theavicaster/e261acaed1e7729ab31e39d7c4f42247/sf40attention.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "swZ6S-TiOXu6"
},
"source": [
"!pip install --upgrade mlxtend\n",
"%tensorflow_version 1.x\n",
"!pip install gdown --pre"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "dWh8mViaOsRs"
},
"source": [
"!gdown https://drive.google.com/uc?id=1-tkK0y3WfBMPaJO5ecRV9g-VvEpyUPgR"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "3Jjy5OQotfka"
},
"source": [
"!unzip stanford40.zip"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "HqWnS4QSP3_1"
},
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "qSu1XQ8F9ROk"
},
"source": [
"import shutil\n",
"shutil.move(\"/content/40class/train/rowing_a_boat\", \"/content/nonbodymotion/train\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "iaa3TQflPW8x"
},
"source": [
"train_dir = '/content/40class/train'\n",
"test_dir = '/content/40class/test'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "p6luLh3nPZiO"
},
"source": [
"import numpy as np\n",
"import os\n",
"from glob import glob\n",
"import math\n",
"import tensorflow as tf\n",
"from keras import optimizers\n",
"from keras.models import Sequential\n",
"from keras.models import load_model\n",
"from keras.models import Model\n",
"from keras.layers import Dense\n",
"from keras.layers import Dropout\n",
"from keras.layers import SpatialDropout2D\n",
"from keras.layers import Flatten, concatenate, GlobalAveragePooling2D\n",
"from keras.layers import Input\n",
"from keras.layers import BatchNormalization\n",
"from keras.layers import Lambda\n",
"from keras.layers import Concatenate\n",
"from keras.layers.advanced_activations import LeakyReLU\n",
"from keras.layers.convolutional import Conv2D\n",
"from keras.layers.convolutional import MaxPooling2D\n",
"from keras.layers import Permute\n",
"from keras.regularizers import l2\n",
"from keras.callbacks import ModelCheckpoint\n",
"from keras.callbacks import LearningRateScheduler\n",
"from keras.callbacks import ReduceLROnPlateau\n",
"from keras.callbacks import CSVLogger\n",
"from keras import layers, models\n",
"from keras import backend as K\n",
"import matplotlib.pyplot as plt"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "NgvxZii2Pehm"
},
"source": [
"data_format = K.image_data_format()\n",
"K.set_image_data_format(data_format)\n",
"np.random.seed(7)\n",
"\n",
"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_KqZt98KPhIq"
},
"source": [
"num_of_classes = 40"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "NwRC9LvLPjJR"
},
"source": [
"from keras.applications import VGG16, VGG19"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "gXOvY08lPsFM"
},
"source": [
"from keras.applications import DenseNet201\n",
"\n",
"model = VGG19(\n",
" include_top=False,\n",
" weights=\"imagenet\",\n",
" input_shape=(224,224,3),\n",
")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "k8FJftGed9Zf"
},
"source": [
"model.summary()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "oi1gd6vJPw17"
},
"source": [
"conv_base = Model(inputs=model.inputs, outputs=model.get_layer('block5_pool').output)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "AYU6S-udP1lq"
},
"source": [
"def spatial_attention(input_feature):\n",
" kernel_size = 7\n",
" if K.image_data_format() == \"channels_first\":\n",
" channel = input_feature.shape[1]\n",
" cbam_feature = Permute((2, 3, 1))(input_feature)\n",
" else:\n",
" channel = input_feature.shape[-1]\n",
" cbam_feature = input_feature\n",
"\n",
" avg_pool = Lambda(lambda x: K.mean(x, axis=3, keepdims=True))(cbam_feature)\n",
" assert avg_pool.shape[-1] == 1\n",
" max_pool = Lambda(lambda x: K.max(x, axis=3, keepdims=True))(cbam_feature)\n",
" assert max_pool.shape[-1] == 1\n",
" concat = Concatenate(axis=3)([avg_pool, max_pool])\n",
" assert concat.shape[-1] == 2\n",
" cbam_feature = Conv2D(filters=1,\n",
" kernel_size=kernel_size,\n",
" strides=1,\n",
" padding='same',\n",
" activation='sigmoid',\n",
" kernel_initializer='he_normal',\n",
" use_bias=False)(concat)\n",
" assert cbam_feature.shape[-1] == 1\n",
"\n",
" if K.image_data_format() == \"channels_first\":\n",
" cbam_feature = Permute((3, 1, 2))(cbam_feature)\n",
" x1 = concatenate([input_feature, cbam_feature])\n",
"\n",
" return x1\n",
" # return cbam_feature"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "fAzMfzeaQUa4"
},
"source": [
"def Trunc_VGG(conv_base):\n",
" conv_base.trainable = True\n",
" layer_4th = conv_base.get_layer('block5_pool').output\n",
" attention_module = spatial_attention(layer_4th)\n",
" print(attention_module.shape)\n",
" flatten = Flatten()(attention_module)\n",
" # flatten=GlobalAveragePooling2D()(attention_module)\n",
" dense = Dense(2048, activation='relu')(flatten)\n",
" # dropout = Dropout(0.5)(dense)\n",
" output = BatchNormalization()(dense)\n",
" dense = Dense(1024, activation='relu')(output)\n",
" # dropout = Dropout(0.3)(dense)\n",
" output = BatchNormalization()(dense)\n",
" dense = Dense(512, activation='relu')(output)\n",
" dropout = Dropout(0.3)(dense)\n",
" output = BatchNormalization()(dropout)\n",
" dense = Dense(256, activation='relu')(output)\n",
" dropout = Dropout(0.5)(dense)\n",
" output = BatchNormalization()(dropout)\n",
" dense = Dense(24, activation='relu')(output)\n",
" pred = (Dense(num_of_classes, activation='softmax'))(dense)\n",
" model = Model(inputs=conv_base.inputs, outputs=pred)\n",
" return model"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "lp5X70f7TXuc"
},
"source": [
"model = Trunc_VGG(conv_base)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mpUwPTcgQgZv"
},
"source": [
"model.summary()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ePUN40MUQwRG"
},
"source": [
"sgd = optimizers.SGD(learning_rate = 0.01, momentum = 0.9, clipnorm = 1.0)\n",
"adam = optimizers.Adam(lr=0.001)\n",
"model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QrdNkxNEQ2oR"
},
"source": [
"from keras.preprocessing.image import ImageDataGenerator\n",
"train_datagen = ImageDataGenerator(\n",
" #samplewise_center=True,\n",
" #samplewise_std_normalization=True\n",
" brightness_range=[0.5,1.5],\n",
" rotation_range=10,\n",
" width_shift_range=0.15,\n",
" height_shift_range=0.15,\n",
" shear_range=0.20,\n",
" zoom_range=0.20,\n",
" fill_mode='nearest',\n",
" )\n",
"test_datagen = ImageDataGenerator(\n",
" #samplewise_center=True,\n",
" #samplewise_std_normalization=True\n",
" )\n",
"\n",
"training_set = train_datagen.flow_from_directory(\n",
" train_dir,\n",
" target_size=(224, 224),\n",
" color_mode='rgb',\n",
" batch_size=10,\n",
" class_mode='categorical',\n",
" seed = 7\n",
" )\n",
"\n",
"test_set = test_datagen.flow_from_directory(\n",
" test_dir,\n",
" target_size=(224, 224),\n",
" color_mode='rgb',\n",
" batch_size=10,\n",
" class_mode='categorical',\n",
" shuffle = False, \n",
" seed = 7\n",
" )\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "NhwWXgatQ_tS"
},
"source": [
"checkpoint = ModelCheckpoint(monitor='val_accuracy', verbose=1, filepath='/content/model.h5', save_best_only=True, mode='max')\n",
"reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.7, min_delta = 0.0005,\n",
" patience=20, min_lr=0.001, verbose = 1)\n",
"callbacks_list = [checkpoint,reduce_lr]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "pcmlZTGGRCuu"
},
"source": [
"number_of_epochs = 200"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Y8MJHXP8_Vax"
},
"source": [
"H = model.fit_generator(\n",
" training_set,\n",
" epochs=number_of_epochs,\n",
" steps_per_epoch=300, #No of train images / batch size,\n",
" validation_data = test_set,\n",
" validation_steps = 398, #No of validation images / batch size\n",
" # callbacks=[cb],\n",
" verbose=1,\n",
" callbacks=callbacks_list\n",
" )"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment