def test_generic_loader(): print('Testing generic data loader') parser = NeonArgparser(__doc__) args = parser.parse_args() train_path = os.path.join(args.data_dir, traindir + '-ingested') test_path = os.path.join(args.data_dir, testdir + '-ingested') write_batches(args, train_path, traindir, 0) write_batches(args, test_path, testdir, 1) params = ImageParams(channel_count=3, height=32, width=32) common = dict(media_params=params, target_size=1, nclasses=10) train = DataLoader('train', repo_dir=os.path.join(args.data_dir, 'train'), **common) test = DataLoader('test', repo_dir=os.path.join(args.data_dir, 'test'), **common) err = run(args, train, test) return err
def __init__(self, repo_dir, subj, elecs, validate_mode, training): if type(elecs) is int: elecs = [elecs] nelecs = len(elecs) self.elecs = elecs media_params, set_name_prefix, data_dir = init(repo_dir, validate_mode, training) indexer = Indexer(repo_dir, validate_mode, training) self.loaders = [] for elec in elecs: set_name = set_name_prefix + '-' + str(subj) + '-' + str(elec) index_file = indexer.run(elec, set_name) loader = DataLoader(set_name=set_name, media_params=media_params, index_file=index_file, repo_dir=data_dir, shuffle=training, target_size=1, nclasses=2) self.loaders.append(loader) self.shape_list = list(media_params.get_shape()) self.shape_list[0] = nelecs self.shape = tuple(self.shape_list) datum_size = media_params.datum_size() self.data = self.be.iobuf(nelecs*datum_size, dtype=np.float32) self.data_shape = (nelecs, datum_size, -1) self.data_view = self.data.reshape(self.data_shape) self.ndata = self.loaders[0].ndata
shape = dict(channel_count=3, height=112, width=112, scale_min=128, scale_max=128) testParams = VideoParams(frame_params=ImageParams(center=True, flip=False, **shape), frames_per_clip=16) common = dict(target_size=1, nclasses=101, datum_dtype=np.uint8) test_set = DataLoader(set_name='val', repo_dir=testdir, media_params=testParams, shuffle=False, **common) def get_model_pred(model_file, dataset): # model creation model = create_network() model.load_params(model_file) model.initialize(dataset=dataset) # pred will have shape (num_clips, num_classes) and contain class probabilities pred = model.get_outputs(test_set) return pred def accumulate_video_pred(pred):
from neon.transforms import Rectlin, Softmax, CrossEntropyMulti, Misclassification from neon.models import Model from neon.data import DataLoader, AudioParams from neon.callbacks.callbacks import Callbacks from util import create_index_files, display parser = NeonArgparser(__doc__) args = parser.parse_args() train_idx, valid_idx = create_index_files(args.data_dir) common_params = dict(sampling_freq=22050, clip_duration=16000, frame_duration=16) train_params = AudioParams(**common_params) valid_params = AudioParams(**common_params) common = dict(target_size=1, nclasses=10, repo_dir=args.data_dir) train = DataLoader(set_name='music-train', media_params=train_params, index_file=train_idx, shuffle=True, **common) valid = DataLoader(set_name='music-valid', media_params=valid_params, index_file=valid_idx, shuffle=False, **common) init = Gaussian(scale=0.01) layers = [Conv((2, 2, 4), init=init, activation=Rectlin(), strides=dict(str_h=2, str_w=4)), Pooling(2, strides=2), Conv((3, 3, 4), init=init, batch_norm=True, activation=Rectlin(), strides=dict(str_h=1, str_w=2)), DeepBiRNN(128, init=GlorotUniform(), batch_norm=True, activation=Rectlin(), reset_cells=True, depth=3), RecurrentMean(), Affine(nout=common['nclasses'], init=init, activation=Softmax())] model = Model(layers=layers) opt = Adagrad(learning_rate=0.01, gradient_clip_value=15)
subj = int(data_dir[-2]) assert subj in [1, 2, 3] indexer = Indexer() tain_idx, test_idx = indexer.run(data_dir, pattern, testing=args.test_mode) fs = 400 cd = 240000 * 1000 / fs common_params = dict(sampling_freq=fs, clip_duration=cd, frame_duration=512) tain_params = AudioParams(random_scale_percent=5.0, **common_params) test_params = AudioParams(**common_params) common = dict(target_size=1, nclasses=2) tain_set = 'full' if args.test_mode else 'tain' test_set = 'test' if args.test_mode else 'eval' test_dir = data_dir.replace('train', 'test') if args.test_mode else data_dir tain = DataLoader(set_name=tain_set, media_params=tain_params, index_file=tain_idx, repo_dir=data_dir, **common) test = DataLoader(set_name=test_set, media_params=test_params, index_file=test_idx, repo_dir=test_dir, **common) gauss = Gaussian(scale=0.01) glorot = GlorotUniform() tiny = dict(str_h=1, str_w=1) small = dict(str_h=1, str_w=2) big = dict(str_h=1, str_w=4) common = dict(batch_norm=True, activation=Rectlin()) layers = [Conv((3, 5, 64), init=gauss, activation=Rectlin(), strides=big), Pooling(2, strides=2), Conv((3, 3, 128), init=gauss, strides=small, **common), Pooling(2, strides=2), Conv((3, 3, 256), init=gauss, strides=small, **common), Conv((2, 2, 512), init=gauss, strides=tiny, **common), DeepBiRNN(128, init=glorot, reset_cells=True, depth=3, **common),
train_dir = os.path.join(args.data_dir, 'train') common_params = dict(sampling_freq=2000, clip_duration=1700, frame_duration=64, overlap_percent=50) train_params = AudioParams(noise_index_file=noise_idx, noise_dir=train_dir, **common_params) test_params = AudioParams(**common_params) common = dict(target_size=1, nclasses=2) # Validate... train = DataLoader(set_name='train', repo_dir=train_dir, media_params=train_params, index_file=train_idx, **common) test = DataLoader(set_name='val', repo_dir=train_dir, media_params=test_params, index_file=val_idx, **common) model = run(train, test) print('Misclassification error = %.1f%%' % (model.eval(test, metric=Misclassification()) * 100)) # model.save_params("whale_calls.prm") # Test... # test_dir = os.path.join(args.data_dir, 'test')
return train_idx, val_idx parser = NeonArgparser(__doc__) args = parser.parse_args() train_idx, val_idx = create_index_files(args.data_dir) common_params = dict(sampling_freq=22050, clip_duration=31000, frame_duration=16) train_params = AudioParams(random_scale_percent=5, **common_params) val_params = AudioParams(**common_params) common = dict(target_size=1, nclasses=10, repo_dir=args.data_dir) train = DataLoader(set_name='genres-train', media_params=train_params, index_file=train_idx, shuffle=True, **common) val = DataLoader(set_name='genres-val', media_params=val_params, index_file=val_idx, shuffle=False, **common) init = Gaussian(scale=0.01) layers = [ Conv((7, 7, 32), init=init, activation=Rectlin(), strides=dict(str_h=2, str_w=4)), Pooling(2, strides=2), Conv((5, 5, 64),
import os from neon.util.argparser import NeonArgparser from neon.util.persist import load_obj from neon.transforms import Misclassification, CrossEntropyMulti from neon.optimizers import GradientDescentMomentum from neon.layers import GeneralizedCost from neon.models import Model from neon.data import DataLoader, ImageParams # parse the command line arguments (generates the backend) parser = NeonArgparser(__doc__) args = parser.parse_args() # setup data provider test_dir = os.path.join(args.data_dir, 'val') shape = dict(channel_count=3, height=32, width=32) test_params = ImageParams(center=True, flip=False, **shape) common = dict(target_size=1, nclasses=10) test_set = DataLoader(set_name='val', repo_dir=test_dir, media_params=test_params, **common) model = Model(load_obj(args.model_file)) cost = GeneralizedCost(costfunc=CrossEntropyMulti()) opt = GradientDescentMomentum(0.1, 0.9, wdecay=0.0001) model.initialize(test_set, cost=cost) acc = 1.0 - model.eval(test_set, metric=Misclassification())[0] print 'Accuracy: %.1f %% (Top-1)' % (acc*100.0) model.benchmark(test_set, cost=cost, optimizer=opt)
scale_max=128) trainParams = VideoParams(frame_params=ImageParams(center=False, flip=True, **shape), frames_per_clip=16) testParams = VideoParams(frame_params=ImageParams(center=True, flip=False, **shape), frames_per_clip=16) common = dict(target_size=1, nclasses=101, datum_dtype=np.uint8) train = DataLoader(set_name='train', repo_dir=traindir, media_params=trainParams, shuffle=True, subset_percent=args.subset_pct, **common) test = DataLoader(set_name='val', repo_dir=testdir, media_params=testParams, shuffle=False, **common) # model creation model = create_network() # setup callbacks callbacks = Callbacks(model, eval_set=test, **args.callback_args) # gradient descent with momentum, weight decay, and learning rate decay schedule
args = parser.parse_args() train_dir = os.path.join(args.data_dir, 'train') test_dir = os.path.join(args.data_dir, 'val') if not (os.path.exists(train_dir) and os.path.exists(test_dir)): extract_images(args.data_dir, 40) # setup data provider shape = dict(channel_count=3, height=32, width=32) train_params = ImageParams(center=False, flip=True, aspect_ratio=110, **shape) test_params = ImageParams(**shape) common = dict(target_size=1, nclasses=10) train = DataLoader(set_name='train', repo_dir=train_dir, media_params=train_params, shuffle=True, subset_percent=args.subset_pct, **common) test = DataLoader(set_name='val', repo_dir=test_dir, media_params=test_params, **common) tune_set = DataLoader(set_name='train', repo_dir=train_dir, media_params=train_params, subset_percent=20, **common) # Structure of the deep residual part of the network: # args.depth modules of 2 convolutional layers each at feature map depths of 16, 32, 64 nfms = [2**(stage + 4) for stage in sorted(list(range(3)) * args.depth)]
parser.add_argument('--model_weights', help='Pickle file of trained model weights.') args = parser.parse_args(gen_be=False) # setup backend be = gen_backend(**extract_valid_args(args, gen_backend)) be.bsz = 32 # setup data provider shape = dict(channel_count=3, height=112, width=112, scale_min=128, scale_max=128) testParams = VideoParams(frame_params=ImageParams(center=True, flip=False, **shape), frames_per_clip=16) common = dict(target_size=1, nclasses=101, datum_dtype=np.uint8) videos = DataLoader(set_name='val', repo_dir=args.data_dir, media_params=testParams, shuffle=False, **common) # initialize model model = create_network() model.load_params(args.model_weights) model.initialize(dataset=videos) # read label index file into dictionary label_index = {} with open(args.class_ind_file) as label_index_file: for line in label_index_file: index, label = line.split() label_index[int(index)-1] = label def print_label_on_image(frame, top_labels):
path = os.path.join(img_dir, str(labels[idx][0]), str(idx) + '.png') im.save(path, format='PNG') # setup data provider train_dir = os.path.join(args.data_dir, 'train') test_dir = os.path.join(args.data_dir, 'val') if not (os.path.exists(train_dir) and os.path.exists(test_dir)): extract_images(args.data_dir, 40) # setup data provider shape = dict(channel_count=3, height=32, width=32) train_params = ImageParams(center=False, flip=True, **shape) test_params = ImageParams(**shape) common = dict(target_size=1, nclasses=10) train = DataLoader(set_name='train', repo_dir=train_dir, media_params=train_params, shuffle=True, **common) test = DataLoader(set_name='val', repo_dir=test_dir, media_params=test_params, **common) def conv_params(fsize, nfm, stride=1, relu=True): return dict(fshape=(fsize, fsize, nfm), strides=stride, padding=(1 if fsize > 1 else 0), activation=(Rectlin() if relu else None), init=Kaiming(local=True), batch_norm=True) def id_params(nfm): return dict(fshape=(1, 1, nfm), strides=2, padding=0, activation=None, init=IdentityInit()) def module_factory(nfm, stride=1):