Skip to content

Instantly share code, notes, and snippets.

View juliensimon's full-sized avatar

Julien Simon juliensimon

View GitHub Profile
@juliensimon
juliensimon / mxnet_example1.py
Last active June 23, 2018 14:14
MXNet example
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
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)
@juliensimon
juliensimon / mxnet_example2.py
Created April 14, 2017 22:57
MXNet + Inception v3
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)
@juliensimon
juliensimon / mxnet_example3.py
Last active November 21, 2018 22:08
MXNet + 3 CNNs
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)
@juliensimon
juliensimon / extractData.py
Last active April 18, 2017 11:48
MNIST - extract labels and images
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
@juliensimon
juliensimon / extractCifar10.py
Last active May 26, 2021 11:27
Extract 10 images from the CIFAR-10 data set
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))
@juliensimon
juliensimon / loadCifar10NDA.py
Last active April 24, 2017 00:07
Load CIFAR-10 in NDArrays
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
@juliensimon
juliensimon / loadRecordIO.py
Last active April 24, 2017 08:42
Loading RecordIO files
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))
@juliensimon
juliensimon / LoadResnext101.py
Last active April 24, 2017 08:42
Load ResNext-101
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)
@juliensimon
juliensimon / loadResnextAdadelta.py
Last active April 28, 2017 12:36
Build a new ResNext-101 on CIFAR-10 with AdaDelta
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