This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
import numpy as np | |
import logging | |
logging.basicConfig(level=logging.INFO) | |
sample_count = 1000 | |
train_count = 800 | |
valid_count = sample_count - train_count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
def splitRGBImage(filename): | |
img = cv2.imread(filename) | |
red = np.copy(img) | |
red[:,:,0].fill(0) | |
red[:,:,1].fill(0) | |
red = cv2.resize(red, (224, 224,)) | |
cv2.imwrite("red_"+filename, red) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
import numpy as np | |
import cv2 | |
from collections import namedtuple | |
def loadInceptionv3(): | |
sym, arg_params, aux_params = mx.model.load_checkpoint('Inception-BN', 0) | |
mod = mx.mod.Module(symbol=sym) | |
mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))]) | |
mod.set_params(arg_params, aux_params) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
import numpy as np | |
import cv2,sys,time | |
from collections import namedtuple | |
def loadModel(modelname): | |
t1 = time.time() | |
sym, arg_params, aux_params = mx.model.load_checkpoint(modelname, 0) | |
t2 = time.time() | |
t = 1000*(t2-t1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
import numpy as np | |
import cv2 | |
labelfile = open("train-labels-idx1-ubyte") | |
# Read packed structure - big-endian, 2 integers: a magic number and the number of labels | |
magic, num = struct.unpack(">II", labelfile.read(8)) | |
labelarray = np.fromstring(labelfile.read(), dtype=np.int8) | |
print labelarray.shape | |
# Print first labels |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
import numpy as np | |
import cPickle | |
import cv2 | |
def extractImagesAndLabels(path, file): | |
f = open(path+file, 'rb') | |
dict = cPickle.load(f) | |
images = dict['data'] | |
images = np.reshape(images, (10000, 3, 32, 32)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
def buildTrainingSet(path): | |
training_data = [] | |
training_label = [] | |
for f in ("data_batch_1", "data_batch_2", "data_batch_3", "data_batch_4", "data_batch_5"): | |
imgarray, lblarray = extractImagesAndLabels(path, f) | |
if not training_data: | |
training_data = imgarray | |
training_label = lblarray |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
train_iter = mx.io.ImageRecordIter( | |
path_imgrec="cifar10_train.rec", data_name="data", label_name="softmax_label", | |
batch_size=batch, data_shape=(3,28,28), shuffle=True) | |
valid_iter = mx.io.ImageRecordIter( | |
path_imgrec="cifar10_val.rec", data_name="data", label_name="softmax_label", | |
batch_size=batch, data_shape=(3,28,28)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
epochs = 100 | |
sym, arg_params, aux_params = mx.model.load_checkpoint("resnext-101",0) | |
mod = mx.mod.Module(symbol=sym, context=(mx.gpu(0), mx.gpu(1), mx.gpu(2), mx.gpu(3))) | |
mod.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label) | |
mod.set_params(arg_params, aux_params) | |
mod.fit(train_iter, eval_data=valid_iter, | |
optimizer_params={'learning_rate':0.05, 'momentum':0.9}, num_epoch=epochs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mxnet as mx | |
import numpy as np | |
import cv2, cPickle, logging | |
from symbols import resnext | |
logging.basicConfig(level=logging.DEBUG) | |
path="cifar-10-batches-py/" | |
examples=50000 | |
batch=128 |