def get_model(self): if self.encoder_type == "HC": self.model = HarmonicCNN() elif self.encoder_type == "SC": self.model = resemul(block_type=self.block) elif self.encoder_type == "MS": self.model = resemul(ms=True, block_type="rese")
def get_model(self): if self.encoder_type == "HC": self.wave_encoder = WaveEncoder() elif self.encoder_type == "SC": self.wave_encoder = resemul(block_type=self.block) elif self.encoder_type == "MS": self.wave_encoder = resemul(ms=True, block_type="rese") self.wave_projector = WaveProjector(128) self.classifier = SupervisedClassifier() self.tag_encoder = TagEncoder() self.tag_decoder = TagDecoder(50)
def get_model(weights): """ Define the model arch + load weights """ # network arch print("Creating model..") input_tensor = Input(shape=(16000, )) output_tensor = resemul(input_tensor, block_type="rese", init_features=128, amplifying_ratio=16, drop_rate=0.5, weight_decay=0.0, num_classes=31) model = Model(input_tensor, output_tensor) sgd = optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True) model.compile(sgd, "categorical_crossentropy", metrics=["accuracy"]) print("Model compiled.") model.load_weights(weights) print("weights loaded..") return model
from utils import data_generator from kaggle_utils import score_generator import sys import os from fgsm_callback import fgsm_callback try: import cPickle as pickle except: import pickle # network arch input_tensor = Input(shape=(16000, )) output_tensor = resemul(input_tensor, block_type="rese", init_features=128, amplifying_ratio=16, drop_rate=0.5, weight_decay=0.0, num_classes=31) model = Model(input_tensor, output_tensor) print(model.summary()) sgd = optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True) model.compile(sgd, "categorical_crossentropy", metrics=["accuracy"]) print("Model compiled.") # scoring if sys.argv[1] == "score": model.load_weights( "/home/tejaswin.p/Can-You-Hear-Me/checkpoints/2018-06-22_08:40:45.299850/weights.04-0.25.hdf5" ) print("Weights loaded.")
def train(initial_lr, stage_train_dir, ckpt_path=None, initial_epoch=0): # Prepare TFRecord batches. x_train, y_train = batch_inputs( file_pattern=make_path(args.data_dir, 'tfrecord', 'train-????-of-????.tfrecord'), batch_size=args.batch_size, is_training=True, num_read_threads=args.num_read_threads, examples_per_shard=args.num_audios_per_shard * args.num_segments_per_audio, shard_queue_name='train_filename_queue', example_queue_name='train_input_queue') x_val, y_val = batch_inputs(file_pattern=make_path( args.data_dir, 'tfrecord', 'val-????-of-????.tfrecord'), batch_size=args.batch_size, is_training=False, num_read_threads=1, examples_per_shard=args.num_audios_per_shard * args.num_segments_per_audio, shard_queue_name='val_filename_queue', example_queue_name='val_input_queue') # Create a model. out = resemul(x_train, block_type=args.block, multi=not args.no_multi, amplifying_ratio=args.alpha, drop_rate=args.dropout, weight_decay=args.weight_decay) model = TFRecordModel(inputs=x_train, val_inputs=x_val, outputs=out) # Load weights from a checkpoint if exists. if ckpt_path: print('=> Load weights from "{}".'.format(ckpt_path)) model.load_weights(ckpt_path) # Setup an optimizer and compile the models. optimizer = SGD(lr=initial_lr, momentum=args.momentum, decay=1e-6, nesterov=True) model.compile_tfrecord(optimizer, loss='binary_crossentropy', y=y_train, y_val=y_val) # --- Setup callbacks. # Setup a TensorBoard callback. tensor_board = TensorBoard(log_dir=stage_train_dir) # Use early stopping mechanism. early_stopping = EarlyStopping(monitor='val_loss', patience=args.patience) # Setup a checkpointer. checkpointer = ModelCheckpoint(make_path( stage_train_dir, 'ckpt-e{epoch:03d}-l{val_loss:.4f}.hdf5'), monitor='val_loss', save_best_only=True) # Setup a CSV logger. csv_logger = CSVLogger(make_path(stage_train_dir, 'training.csv'), append=True) # Kick-off the training! num_train, num_val, num_test = 15250, 1529, 4332 train_steps = calculate_steps(num_train, args.num_segments_per_audio, args.batch_size) val_steps = calculate_steps(num_val, args.num_segments_per_audio, args.batch_size) model.fit_tfrecord( epochs=100, initial_epoch=initial_epoch, steps_per_epoch=train_steps, validation_steps=val_steps, callbacks=[tensor_board, early_stopping, checkpointer, csv_logger]) # The end of the stage. Evaluate on test set. best_ckpt_path, *_ = find_best_checkpoint(stage_train_dir) print( '=> The end of the stage. Start evaluation on test set using checkpoint "{}".' .format(best_ckpt_path)) evaluate(model, args.data_dir, num_test, args.num_audios_per_shard)