4Moyede
.vscode/
__pycache__/
trained/
src/
from keras.models import Model
from keras.layers import Input, Reshape
from keras.layers.core import Dense, Lambda, Activation
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import GlobalAveragePooling2D, MaxPooling2D
from keras.layers.merge import concatenate, add, multiply
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
from keras.utils.data_utils import get_file
from keras_applications.imagenet_utils import _obtain_input_shape
import keras.backend as K
import os
class SEResNeXt(Model):
def __init__(self, weight, input_shape=None):
'''
ResNext Model
## Args
+ weight:
+ input_shape: optional shape tuple
'''
if weights not in {'cifar10', 'imagenet'}:
raise ValueError
self.__weight = weight
if weight == 'cifar10':
self.__depth = 29
self.__cardinality = 8
self.__width = 64
self.__classes = 10
else:
self.__depth = [3, 8, 36, 3]
self.__cardinality = 32
self.__width = 4
self.__classes = 1000
self.__reduction_ratio = 4
self.__weight_decay = 5e-4
self.__channel_axis = 1 if K.image_data_format() == "channels_first" else -1
if weight == 'cifar10':
self.__input_shape = _obtain_input_shape(input_shape, default_size=32, min_size=8, data_format=K.image_data_format(), require_flatten=True)
else:
self.__input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=112, data_format=K.image_data_format(), require_flatten=True)
self.__img_input = Input(shape=self.__input_shape)
# Create model.
super(SEResNeXt, self).__init__(self.__img_input, self.__create_res_next(), name='seresnext')
def __initial_conv_block(self):
'''
Adds an initial conv block, with batch norm and relu for the inception resnext
'''
if weight == 'cifar10':
x = Conv2D(64, (3, 3), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(self.__weight_decay))(self.__img_input)
x = BatchNormalization(axis=self.__channel_axis)(x)
x = Activation('relu')(x)
return x
else:
x = Conv2D(64, (7, 7), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(self.__weight_decay), strides=(2, 2))(self.__img_input)
x = BatchNormalization(axis=self.__channel_axis)(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
return x
def __grouped_convolution_block(self, input, grouped_channels, strides):
'''
Adds a grouped convolution block. It is an equivalent block from the paper
## Args
+ input: input tensor
+ grouped_channels: grouped number of filters
+ strides: performs strided convolution for downscaling if > 1
## Returns
a keras tensor
'''
init = input
group_list = []
for c in range(self.__cardinality):
x = Lambda(lambda z: z[:, :, :, c * grouped_channels:(c + 1) * grouped_channels])(input)
x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=(strides, strides), kernel_initializer='he_normal', kernel_regularizer=l2(self.__weight_decay))(x)
group_list.append(x)
group_merge = concatenate(group_list, axis=self.__channel_axis)
x = BatchNormalization(axis=self.__channel_axis)(group_merge)
x = Activation('relu')(x)
return x
def __bottleneck_block(self, input, filters=64, strides=1):
'''
Adds a bottleneck block
## Args
+ input: input tensor
+ filters: number of output filters
+ strides: performs strided convolution for downsampling if > 1
## Returns
a keras tensor
'''
init = input
grouped_channels = int(filters / self.__cardinality)
# Check if input number of filters is same as 16 * k, else create convolution2d for this input
if init._keras_shape[-1] != 2 * filters:
init = Conv2D(filters * 2, (1, 1), padding='same', strides=(strides, strides), use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(self.__weight_decay))(init)
init = BatchNormalization(axis=self.__channel_axis)(init)
x = Conv2D(filters, (1, 1), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(self.__weight_decay))(input)
x = BatchNormalization(axis=self.__channel_axis)(x)
x = Activation('relu')(x)
x = self.__squeeze_excitation_layer(x, x[0].get_shape()[self.__channel_axis])
x = self.__grouped_convolution_block(x, grouped_channels, strides)
x = Conv2D(filters * 2, (1, 1), padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(self.__weight_decay))(x)
x = BatchNormalization(axis=self.__channel_axis)(x)
x = add([init, x])
x = Activation('relu')(x)
return x
def __create_res_next(self):
'''
Creates a ResNeXt model with specified parameters
'''
if type(self.__depth) is list or type(self.__depth) is tuple:
N = list(self.__depth)
else:
N = [(self.__depth - 2) // 9 for _ in range(3)]
print(N)
filters = self.__cardinality * self.__width
filters_list = []
for i in range(len(N)):
filters_list.append(filters)
filters *= 2 # double the size of the filters
x = self.__initial_conv_block()
# block 1 (no pooling)
for i in range(N[0]):
x = self.__bottleneck_block(x, filters_list[0], strides=1)
N = N[1:] # remove the first block from block definition list
filters_list = filters_list[1:] # remove the first filter from the filter list
# block 2 to N
for block_idx, n_i in enumerate(N):
for i in range(n_i):
if i == 0:
x = self.__bottleneck_block(x, filters_list[block_idx], strides=2)
else:
x = self.__bottleneck_block(x, filters_list[block_idx], strides=1)
x = GlobalAveragePooling2D()(x)
x = Dense(self.__classes, use_bias=False, kernel_regularizer=l2(self.__weight_decay), kernel_initializer='he_normal', activation='softmax')(x)
return x
def __squeeze_excitation_layer(self, x, out_dim):
'''
SE Block Function
## Args
+ x : input feature map
+ out_dim : dimention of output channel
'''
squeeze = GlobalAveragePooling2D()(x)
excitation = Dense(units=out_dim // self.__reduction_ratio)(squeeze)
excitation = Activation('relu')(excitation)
excitation = Dense(units=out_dim)(excitation)
excitation = Activation('sigmoid')(excitation)
excitation = Reshape((1,1,out_dim))(excitation)
scale = multiply([x,excitation])
return scale
if __name__ == '__main__':
model = SEResNeXt((112, 112, 3))
model.summary()
alabaster==0.7.12
anaconda-client==1.7.2
anaconda-navigator==1.9.12
anaconda-project==0.8.3
argh==0.26.2
asn1crypto==1.3.0
astroid==2.3.3
astropy==4.0
atomicwrites==1.3.0
attrs==19.3.0
autopep8==1.4.4
Babel==2.8.0
backcall==0.1.0
backports.functools-lru-cache==1.6.1
backports.shutil-get-terminal-size==1.0.0
backports.tempfile==1.0
backports.weakref==1.0.post1
beautifulsoup4==4.8.2
bitarray==1.2.1
bkcharts==0.2
bleach==3.1.0
bokeh==1.4.0
boto==2.49.0
Bottleneck==1.3.2
certifi==2019.11.28
cffi==1.14.0
chardet==3.0.4
Click==7.0
cloudpickle==1.3.0
clyent==1.2.2
colorama==0.4.3
conda==4.8.3
conda-build==3.18.11
conda-package-handling==1.7.0
conda-verify==3.4.2
contextlib2==0.6.0.post1
cryptography==2.8
cycler==0.10.0
Cython==0.29.15
cytoolz==0.10.1
dask==2.11.0
decorator==4.4.1
defusedxml==0.6.0
diff-match-patch==20181111
distributed==2.11.0
docutils==0.16
entrypoints==0.3
et-xmlfile==1.0.1
fastcache==1.1.0
filelock==3.0.12
flake8==3.7.9
Flask==1.1.1
fsspec==0.6.2
future==0.18.2
gevent==1.4.0
glob2==0.7
gmpy2==2.0.8
greenlet==0.4.15
h5py==2.10.0
HeapDict==1.0.1
html5lib==1.0.1
hypothesis==5.5.4
idna==2.8
imageio==2.6.1
imagesize==1.2.0
importlib-metadata==1.5.0
intervaltree==3.0.2
ipykernel==5.1.4
ipython==7.12.0
ipython-genutils==0.2.0
ipywidgets==7.5.1
isort==4.3.21
itsdangerous==1.1.0
jdcal==1.4.1
jedi==0.14.1
jeepney==0.4.2
Jinja2==2.11.1
joblib==0.14.1
json5==0.9.1
jsonschema==3.2.0
jupyter==1.0.0
jupyter-client==5.3.4
jupyter-console==6.1.0
jupyter-core==4.6.1
jupyterlab==1.2.6
jupyterlab-server==1.0.6
keyring==21.1.0
kiwisolver==1.1.0
lazy-object-proxy==1.4.3
libarchive-c==2.8
lief==0.9.0
llvmlite==0.31.0
locket==0.2.0
lxml==4.5.0
MarkupSafe==1.1.1
matplotlib==3.1.3
mccabe==0.6.1
mistune==0.8.4
mkl-fft==1.0.15
mkl-random==1.1.0
mkl-service==2.3.0
mock==4.0.1
more-itertools==8.2.0
mpmath==1.1.0
msgpack==0.6.1
multipledispatch==0.6.0
navigator-updater==0.2.1
nbconvert==5.6.1
nbformat==5.0.4
networkx==2.4
nltk==3.4.5
nose==1.3.7
notebook==6.0.3
numba==0.48.0
numexpr==2.7.1
numpy==1.18.1
numpydoc==0.9.2
olefile==0.46
openpyxl==3.0.3
packaging==20.1
pandas==1.0.1
pandocfilters==1.4.2
parso==0.5.2
partd==1.1.0
path==13.1.0
pathlib2==2.3.5
pathtools==0.1.2
patsy==0.5.1
pep8==1.7.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==7.0.0
pkginfo==1.5.0.1
pluggy==0.13.1
ply==3.11
prometheus-client==0.7.1
prompt-toolkit==3.0.3
psutil==5.6.7
ptyprocess==0.6.0
py==1.8.1
pycodestyle==2.5.0
pycosat==0.6.3
pycparser==2.19
pycrypto==2.6.1
pycurl==7.43.0.5
pydocstyle==4.0.1
pyflakes==2.1.1
Pygments==2.5.2
pylint==2.4.4
pyodbc===4.0.0-unsupported
pyOpenSSL==19.1.0
pyparsing==2.4.6
pyrsistent==0.15.7
PySocks==1.7.1
pytest==5.3.5
pytest-arraydiff==0.3
pytest-astropy==0.8.0
pytest-astropy-header==0.1.2
pytest-doctestplus==0.5.0
pytest-openfiles==0.4.0
pytest-remotedata==0.3.2
python-dateutil==2.8.1
python-jsonrpc-server==0.3.4
python-language-server==0.31.7
pytz==2019.3
PyWavelets==1.1.1
pyxdg==0.26
PyYAML==5.3
pyzmq==18.1.1
QDarkStyle==2.8
QtAwesome==0.6.1
qtconsole==4.6.0
QtPy==1.9.0
requests==2.22.0
rope==0.16.0
Rtree==0.9.3
ruamel-yaml==0.15.87
scikit-image==0.16.2
scikit-learn==0.22.1
scipy==1.4.1
seaborn==0.10.0
SecretStorage==3.1.2
Send2Trash==1.5.0
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.14.0
snowballstemmer==2.0.0
sortedcollections==1.1.2
sortedcontainers==2.1.0
soupsieve==1.9.5
Sphinx==2.4.0
sphinxcontrib-applehelp==1.0.1
sphinxcontrib-devhelp==1.0.1
sphinxcontrib-htmlhelp==1.0.2
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.2
sphinxcontrib-serializinghtml==1.1.3
sphinxcontrib-websupport==1.2.0
spyder==4.0.1
spyder-kernels==1.8.1
SQLAlchemy==1.3.13
statsmodels==0.11.0
sympy==1.5.1
tables==3.6.1
tblib==1.6.0
terminado==0.8.3
testpath==0.4.4
toolz==0.10.0
tornado==6.0.3
tqdm==4.42.1
traitlets==4.3.3
ujson==1.35
unicodecsv==0.14.1
urllib3==1.25.8
watchdog==0.10.2
wcwidth==0.1.8
webencodings==0.5.1
Werkzeug==1.0.0
widgetsnbextension==3.5.1
wrapt==1.11.2
wurlitzer==2.0.0
xlrd==1.2.0
XlsxWriter==1.2.7
xlwt==1.3.0
xmltodict==0.12.0
yapf==0.28.0
zict==1.0.0
zipp==2.2.0
from keras.models import load_model
from keras.datasets import fashion_mnist
import matplotlib.pyplot as plt
import os
MODEL_SAVE_FOLDER_PATH = os.path.join(os.getcwd(), 'trained')
MODEL_SAVE_PATH = MODEL_SAVE_FOLDER_PATH + '/seresnext_cifar10.h5'
TEST_IMAGE_FOLDER_PATH = os.path.join(os.getcwd(), 'test')
TEST_IMAGE_PATH = TEST_IMAGE_FOLDER_PATH + '/test01.png'
model = load_model('MODEL_SAVE_PATH')
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
model.predict(test_images[:1, :])
model.predict_classes(test_images[:1, :], verbose=0)
plt.imshow(test_images[0])
from model import SEResNeXt
from keras.datasets import fashion_mnist
from keras import optimizers
import os
import sys
import tensorflow_datasets as tfds
import os
MODEL_SAVE_FOLDER_PATH = os.path.join(os.getcwd(), 'trained')
if not os.path.exists(MODEL_SAVE_FOLDER_PATH):
os.mkdir(MODEL_SAVE_FOLDER_PATH)
MODEL_SAVE_PATH = MODEL_SAVE_FOLDER_PATH + '/seresnext_cifar.h5'
model = SEResNeXt('cifar-10', (32, 32, 3))
model.compile(optimizer=optimizers.Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# ds_train = tfds.load('imagenet2012_corrupted', split='train', shuffle_files=True)
ds_train = tfds.load('cifar-10', split='train', shuffle_files=True)
ds_train = ds_train.shuffle(1000).batch(128).prefetch(10)
model.fit(ds_train['image'], ds_train['label'], epochs=10, steps_per_epoch=30)
model.save(MODEL_SAVE_PATH)