def load_mobilenet1_0(ctx): return vision.mobilenet1_0(pretrained=True, ctx=ctx, prefix="")
import os import random import time import mxnet as mx import mxnet.ndarray as nd from mxnet.gluon.model_zoo.vision import mobilenet1_0 from mxnet import init, image, autograd, nd from mxnet.image import color_normalize from mxnet.gluon.data.vision import ImageRecordDataset import mxnet.gluon as gluon pretrained_net = mobilenet1_0(pretrained=True) net = mobilenet1_0(classes=2) net.features = pretrained_net.features net.output.initialize(init.Xavier()) license_map = {'d': [], 'n': []} license_to_idx = {'n': 0, 'd': 1} data_dir = "C:\\Users\\Jason\\Documents\\Python Projects\\ml\\hackathon\\licenses mxnet" fnames = os.listdir(data_dir) counter = 0 for fn in fnames: arr = fn.split('_') category = arr[0] license_map[category].append({ 'idx': counter,
train_rec = os.path.join(data_dir, 'train/img.rec') validation_rec = os.path.join(data_dir, 'validation/img.rec') model_out_path = os.path.join(data_dir, '../models/{}.params'.format(model_name)) # load data train_iterator = ImageRecordDataset( filename=train_rec, transform=lambda X, y: transform(X, y, train_augs) ) validation_iterator = ImageRecordDataset( filename=validation_rec, transform=lambda X, y: transform(X, y, test_augs) ) # instantiate source model pretrained_net = mobilenet1_0(pretrained=True, prefix='model_') # instantiate target model net = mobilenet1_0(classes=2, prefix='model_') # transfer non output layers from source model to target model net.features = pretrained_net.features # initializing parameters for output layers of target model net.output.initialize(init.Xavier()) # train target model start = time.time() train(net, train_iterator, validation_iterator, ctx, batch_size=batch_size, epochs=epochs, learning_rate=lr) end = time.time() print('\nTOTAL TIME ELAPSED: {}s\n'.format(end - start)) # save target model's learned parameters net.collect_params().save(model_out_path)
################## # Hyperparameter # #----------------# ctx = mx.cpu() lr = 0.05 epochs = 10 momentum = 0.9 batch_size = 64 #----------------# # Hyperparameter # ################## ################## model from mxnet.gluon.model_zoo import vision net = vision.mobilenet1_0(classes=10, pretrained=False, ctx=ctx) # net = vision.mobilenet0_75(classes=10, pretrained=False, ctx=ctx) # net = vision.mobilenet0_5(classes=10, pretrained=False, ctx=ctx) # net = vision.mobilenet0_25(classes=10, pretrained=False, ctx=ctx) ################## 그래프 import gluoncv inputShape = (1, 3, 224, 224) gluoncv.utils.viz.plot_network(net, shape=inputShape) ##### 전처리 ############################################## def transformer(data, label): data = mx.image.imresize(data, 224, 224) data = mx.nd.transpose(data, (2, 0, 1)) data = data.astype(np.float32)