def learn(self, iteratable_data): ''' Learn samples drawn by `IteratableData.generate_learned_samples()`. Args: iteratable_data: is-a `IteratableData`. ''' if isinstance(iteratable_data, IteratableData) is False: raise TypeError( "The type of `iteratable_data` must be `IteratableData`.") self.__loss_list = [] self.__test_loss_list = [] try: epoch = 0 iter_n = 0 for observed_arr, label_arr, test_observed_arr, test_label_arr in iteratable_data.generate_learned_samples( ): observed_arr = nd.flatten(observed_arr) test_observed_arr = nd.flatten(test_observed_arr) self.batch_size = observed_arr.shape[0] if ((epoch + 1) % self.__attenuate_epoch == 0): self.__learning_rate = self.__learning_rate * self.__learning_attenuate_rate with autograd.record(): visible_activity_arr = self.inference(observed_arr) loss = self.compute_loss(observed_arr, visible_activity_arr) loss.backward() self.trainer.step(observed_arr.shape[0]) self.regularize() if (iter_n + 1) % int( iteratable_data.iter_n / iteratable_data.epochs) == 0: test_visible_activity_arr = self.inference( test_observed_arr) test_loss = self.compute_loss(test_observed_arr, test_visible_activity_arr) self.__loss_list.append(loss.asnumpy()[0]) self.__test_loss_list.append(test_loss.asnumpy()[0]) self.__logger.debug("Epoch: " + str(epoch + 1) + " Train loss: " + str(self.__loss_list[-1]) + " Test loss: " + str(self.__test_loss_list[-1])) epoch += 1 iter_n += 1 except KeyboardInterrupt: self.__logger.debug("Interrupt.") self.__logger.debug("end. ") self.__loss_arr = np.c_[ np.array(self.__loss_list[:len(self.__test_loss_list)]), np.array(self.__test_loss_list)]
def observe_reward_value( self, state_arr, action_arr, meta_data_arr=None, ): ''' Compute the reward value. Args: state_arr: Tensor of state. action_arr: Tensor of action. meta_data_arr: Meta data of actions. Returns: Reward value. ''' if state_arr is not None: mse_arr = nd.mean( nd.square( nd.flatten(state_arr), nd.flatten(action_arr) ), axis=0, exclude=True ) reward_value_arr = 1 / mse_arr reward_value_arr = nd.expand_dims(reward_value_arr, axis=1) else: reward_value_arr = nd.zeros(( action_arr.shape[0], 1 ), ctx=action_arr.context) return reward_value_arr
def observe_reward_value( self, state_arr, action_arr, meta_data_arr=None, ): ''' Compute the reward value. Args: state_arr: Tensor of state. action_arr: Tensor of action. meta_data_arr: Meta data of actions. Returns: Reward value. ''' if state_arr is not None: t_hot_loss = -nd.mean( nd.flatten(state_arr) * nd.flatten(action_arr), axis=0, exclude=True) reward_value_arr = t_hot_loss reward_value_arr = nd.expand_dims(reward_value_arr, axis=1) else: reward_value_arr = nd.zeros((action_arr.shape[0], 1), ctx=action_arr.context) if meta_data_arr is not None: add_reward_arr = nd.zeros((action_arr.shape[0], 1), ctx=action_arr.context) for batch in range(meta_data_arr.shape[0]): keyword = "".join(meta_data_arr[batch].reshape(1, -1).tolist()[0]) reward = 0.0 for i in range(len(self.__txt_list)): key = self.__txt_list[i].index(keyword) reward = reward + ((len(self.__txt_list[i]) - key) / len(self.__txt_list[i])) reward = reward + (self.__txt_list[i].count(keyword) / len(self.__txt_list[i])) add_reward_arr[batch] = reward / len(self.__txt_list) else: add_reward_arr = nd.zeros((meta_data_arr.shape[0], 1), ctx=meta_data_arr.context) reward_value_arr = (reward_value_arr * self.__s_a_dist_weight) + ( add_reward_arr * (1 - self.__s_a_dist_weight)) reward_value_arr = nd.tanh(reward_value_arr) return reward_value_arr
def net(X, verbose=False): X = X.as_in_context(W1.context) # 第一层卷积 h1_conv = nd.Convolution(data=X, weight=W1, bias=b1, kernel=W1.shape[2:], num_filter=W1.shape[0]) h1_activation = nd.relu(h1_conv) h1 = nd.Pooling(data=h1_activation, pool_type='max', kernel=(2, 2), stride=(2, 2)) # 第二层卷积 h2_conv = nd.Convolution(data=h1, weight=W2, bias=b2, kernel=W2.shape[2:], num_filter=W2.shape[0]) h2_activation = nd.relu(h2_conv) h2 = nd.Pooling(h2_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) h2 = nd.flatten(h2) # 第一层全连接 h3_linear = nd.dot(h2, W3) + b3 h3 = nd.relu(h3_linear) # 第二层全连接 h4_linear = nd.dot(h3, W4) + b4 if verbose: print('1st conv block', h1.shape) print('2nd conv block', h2.shape) print('1st conv block', h3.shape) print('2nd conv block', h4_linear.shape) print('output:', h4_linear) return h4_linear
def function_set(self): # 第一层卷积 # 卷积 h1_conv = nd.Convolution( data=self.__batch_X, weight=self.__W1, bias=self.__b1, kernel=self.__W1.shape[2:], num_filter=self.__W1.shape[0]) # 激活 h1_activation = nd.relu(h1_conv) # 池化 h1 = nd.Pooling(data=h1_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) # 第二层卷积 h2_conv = nd.Convolution( data=h1, weight=self.__W2, bias=self.__b2, kernel=self.__W2.shape[2:], num_filter=self.__W2.shape[0]) h2_activation = nd.relu(h2_conv) h2 = nd.Pooling(data=h2_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) h2 = nd.flatten(h2) # 第一层全连接 h3_linear = nd.dot(h2, self.__W3) + self.__b3 h3 = nd.relu(h3_linear) # 第二层全连接 h4_linear = nd.dot(h3, self.__W4) + self.__b4 # print("1st conv block:", h1.shape) # print("2nd conv block:", h2.shape) # print("1st dense:", h3.shape) # print("2nd dense:", h4_linear.shape) # print("output:", h4_linear) return h4_linear
def reformat(x: nd.array) -> np.array: """Reformat array to be (4, ?, ?, ?). Softmax is run across (4, -1) before the array is reshaped to the original dimensions. So, we re-arrange dimensions, keeping bounding box attributes together where needed. """ return nd.flatten(reformat(x)).T.asnumpy()
def function_set(self): def batch_norm(X, gamma, beta, is_training, moving_mean, moving_variance, eps=1e-5, moving_momentum=0.9): assert len(X.shape) in (2, 4) # 全连接: batch_size x feature if len(X.shape) == 2: # 每个输入维度在样本上的平均和方差 mean = X.mean(axis=0) variance = ((X - mean) ** 2).mean(axis=0) # 2D卷积: batch_size x channel x height x width else: # 对每个通道算均值和方差,需要保持 4D 形状使得可以正确的广播 mean = X.mean(axis=(0, 2, 3), keepdims=True) variance = ((X - mean) ** 2).mean(axis=(0, 2, 3), keepdims=True) # 变形使得可以正确的广播 moving_mean = moving_mean.reshape(mean.shape) moving_variance = moving_variance.reshape(mean.shape) # 均一化 if is_training: X_hat = (X - mean) / nd.sqrt(variance + eps) # !!! 更新全局的均值和方差 # 每一个 batch_X 都会使用上个 batch_X 的 0.9 与 这个 batch_X 的 0.1 moving_mean[:] = moving_momentum * moving_mean + (1.0 - moving_momentum) * mean moving_variance[:] = moving_momentum * moving_variance + (1.0 - moving_momentum) * variance else: # !!! 测试阶段使用全局的均值和方差 X_hat = (X - moving_mean) / nd.sqrt(moving_variance + eps) # 拉升和偏移 return gamma.reshape(mean.shape) * X_hat + beta.reshape(mean.shape) # 第一层卷积 h1_conv = nd.Convolution( data=self.__batch_X, weight=self.__W1, bias=self.__b1, kernel=(5, 5), num_filter=20) # 第一个 BN h1_bn = batch_norm( h1_conv, self.__gamma1, self.__beta1, self.__is_training, self.__moving_mean1, self.__moving_variance1) h1_activation = nd.relu(h1_bn) h1 = nd.Pooling( data=h1_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) # 第二层卷积 h2_conv = nd.Convolution( data=h1, weight=self.__W2, bias=self.__b2, kernel=(3, 3), num_filter=50) # 第二个 BN h2_bn = batch_norm( h2_conv, self.__gamma2, self.__beta2, self.__is_training, self.__moving_mean2, self.__moving_variance2) h2_activation = nd.relu(h2_bn) h2 = nd.Pooling(data=h2_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) h2 = nd.flatten(h2) # 第一层全连接 h3_linear = nd.dot(h2, self.__W3) + self.__b3 h3 = nd.relu(h3_linear) # 第二层全连接 h4_linear = nd.dot(h3, self.__W4) + self.__b4 return h4_linear
def network(self, X=None, debug=False,): filters, kernels, stride, padding, dilate = self.conv_params['num_filter'], self.conv_params['kernel'], \ self.conv_params['stride'], self.conv_params['padding'], self.conv_params['dilate'] type_pool, kernels_pool, stride_pool, padding_pool, dilate_pool = self.pool_params['pool_type'], \ self.pool_params['kernel'], self.pool_params['stride'], \ self.pool_params['padding'], self.pool_params['dilate'] act_type = self.act_params['act_type'] hidden_dim = self.fc_params['hidden_dim'] # CNN ########################################################################################################## convlayer_out = X interlayer = [] for i, (nf, k, S, P, D, t_p, k_p, S_p, P_p, D_p, a) in enumerate(zip(filters, kernels, stride, padding, dilate, type_pool, kernels_pool, stride_pool, padding_pool, dilate_pool, act_type)): W, b = self.params['W{:d}'.format(i+1,)], self.params['b{:d}'.format(i+1,)] convlayer_out = nd.Convolution(data = convlayer_out, weight=W, bias=b, kernel=k, num_filter=nf, stride=S, dilate=D) convlayer_out = activation(convlayer_out, act_type = a) convlayer_out = nd.Pooling(data=convlayer_out, pool_type=t_p, kernel=k_p, stride=S_p, pad=P_p) interlayer.append(convlayer_out) i_out = i if debug: print("layer{:d} shape: {}".format(i+1, convlayer_out.shape)) # MLP ########################################################################################################## FClayer_out = nd.flatten(convlayer_out) interlayer.append(FClayer_out) if debug: print("After Flattened, Data shape: {}".format(FClayer_out.shape)) for j, (hd, a) in enumerate(zip(hidden_dim, act_type[-len(hidden_dim):])): W, b = self.params['W{:d}'.format(j+i_out+2,)], self.params['b{:d}'.format(j+i_out+2,)] FClayer_out = nd.dot(FClayer_out, W) + b FClayer_out = activation(FClayer_out, act_type = a) if autograd.is_training(): # 对激活函数的输出使用droupout FClayer_out = dropout(FClayer_out, self.drop_prob) if debug: print("layer{:d} shape: {}".format(j+i_out+2, FClayer_out.shape)) interlayer.append(FClayer_out) j_out = j # OUTPUT ########################################################################################################## W, b = self.params['W{:d}'.format(j_out+i_out+3,)], self.params['b{:d}'.format(j_out+i_out+3,)] yhat = nd.dot(FClayer_out, W) + b if debug: print("Output shape: {}".format(yhat.shape)) print('------------') interlayer.append(yhat) return yhat, interlayer
def update(self, labels: nd.array, preds: nd.array) -> float: metric.check_label_shapes(labels, preds) label = nd.array( reformat(labels[1]).asnumpy().ravel().round().astype(np.int)) pred = nd.flatten(preds[1]).T.as_in_context(label.context) error = nd.softmax_cross_entropy(pred, label).asscalar() self.sum_metric += error self.num_inst += NUM_ANCHORS return error / NUM_ANCHORS
def net(X, verbose=False): X = X.as_in_context(W1.context) # 第一个卷积层 h1_conv = nd.Convolution(data=X, weight=W1, bias=b1, kernel=W1.shape[2:], num_filter=W1.shape[0]) h1_activation = nd.relu(h1_conv) h1 = nd.Pooling(data=h1_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) # h1_conv.shape: (256, 20, 24, 24) # h1.shape: (256, 20, 12, 12) #print('h1_conv.shape: ',h1_conv.shape) #print('h1.shape: ',h1.shape) # 第二个卷积层 h2_conv = nd.Convolution(data=h1, weight=W2, bias=b2, kernel=W2.shape[2:], num_filter=W2.shape[0]) h2_activation = nd.relu(h2_conv) h2 = nd.Pooling(data=h2_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) h2 = nd.flatten(h2) # 第一个全连接层 h3_linear = nd.dot(h2, W3) + b3 h3 = nd.relu(h3_linear) # 第二个全连接层 h4_linear = nd.dot(h3, W4) + b4 if verbose: print('1st conv block:', h1.shape) print('2nd conv block:', h2.shape) print('1st dense block:', h3.shape) print('2nd dense block:', h4_linear.shape) print('output:', h4_linear) return h4_linear
def net(x, is_training=False, verbose=False): x = x.as_in_context(w1.context) h1_conv = nd.Convolution(data=x, weight=w1, bias=b1, kernel=w1.shape[2:], num_filter=c1) h1_bn = utils.batch_norm(h1_conv, gamma1, beta1, is_training, moving_mean1, moving_variance1) h1_activation = nd.relu(h1_conv) h1 = nd.Pooling(data=h1_activation, pool_type='max', kernel=(2, 2), stride=(2, 2)) h2_conv = nd.Convolution(data=h1, weight=w2, bias=b2, kernel=w2.shape[2:], num_filter=c2) h2_bn = utils.batch_norm(h2_conv, gamma2, beta2, is_training, moving_mean2, moving_variance2) h2_activation = nd.relu(h2_conv) h2 = nd.Pooling(data=h2_activation, pool_type='max', kernel=(2, 2), stride=(2, 2)) h2 = nd.flatten(h2) h3_linear = nd.dot(h2, w3) + b3 h3 = nd.relu(h3_linear) h4_linear = nd.dot(h3, w4) + b4 if verbose: print('h1 conv block: ', h1.shape) print('h2 conv block: ', h2.shape) print('h3 conv block: ', h3.shape) print('h4 conv block: ', h4_linear.shape) print('output: ', h4_linear) return h4_linear.as_in_context(ctx)
def network(X,drop_rate=0.0): # formula : output_size=((input−weights+2*Padding)/Stride)+1 #data size # MNIST,FashionMNIST = (batch size , 1 , 28 , 28) # CIFAR = (batch size , 3 , 32 , 32) C_H1=nd.Activation(data= nd.Convolution(data=X , weight = W1 , bias = B1 , kernel=(3,3) , stride=(1,1) , num_filter=60) , act_type="relu") # MNIST : result = ( batch size , 60 , 26 , 26) , CIFAR10 : : result = ( batch size , 60 , 30 , 30) P_H1=nd.Pooling(data = C_H1 , pool_type = "max" , kernel=(2,2), stride = (2,2)) # MNIST : result = (batch size , 60 , 13 , 13) , CIFAR10 : result = (batch size , 60 , 15 , 15) C_H2=nd.Activation(data= nd.Convolution(data=P_H1 , weight = W2 , bias = B2 , kernel=(6,6) , stride=(1,1) , num_filter=30), act_type="relu") # MNIST : result = ( batch size , 30 , 8 , 8), CIFAR10 : result = ( batch size , 30 , 10 , 10) P_H2=nd.Pooling(data = C_H2 , pool_type = "max" , kernel=(2,2), stride = (2,2)) # MNIST : result = (batch size , 30 , 4 , 4) , CIFAR10 : result = (batch size , 30 , 5 , 5) P_H2 = nd.flatten(data=P_H2) '''FullyConnected parameter • data: (batch_size, input_dim) • weight: (num_hidden, input_dim) • bias: (num_hidden,) • out: (batch_size, num_hidden) ''' F_H1 =nd.Activation(nd.FullyConnected(data=P_H2 , weight=W3 , bias=B3 , num_hidden=120),act_type="sigmoid") F_H1 =nd.Dropout(data=F_H1, p=drop_rate) F_H2 =nd.Activation(nd.FullyConnected(data=F_H1 , weight=W4 , bias=B4 , num_hidden=64),act_type="sigmoid") F_H2 =nd.Dropout(data=F_H2, p=drop_rate) softmax_Y = nd.softmax(nd.FullyConnected(data=F_H2 ,weight=W5 , bias=B5 , num_hidden=10)) return softmax_Y
def extract_features(self, iter): print("Lovely extracted features") data_iterator = [] if iter == 'test': data_iterator = self.test_iter elif iter == 'val': data_iterator = self.val_iter else: exit("bad iter selection") # Test Iterator feature_vector_list = [] # ----------- Extraction Loop ---------- for epoch in range(1): print("\n >>>> Processing Images") all_labels = [] for batch_id, batch in enumerate(data_iterator): data = batch.data[0].as_in_context(self.ctx) # Fetch batch image data labels = batch.label[0].as_in_context(self.ctx) # Fetch ground truth labels out = self.NNet_Model(data) output = nd.flatten(out).asnumpy().tolist() # Obtain features all_labels.extend(labels.asnumpy()) # Extend label list for single_output_item in output: # Append output features to feature list feature_vector_list.append(single_output_item) # Display check if batch_id % 5 == 0: print(" >> Images Processed: " + str(batch_id * self.batch_size)) write_features_to_file(feature_vector_list, all_labels, self.feature_file)
def net_lenet(X, verbose=False): # 第一层卷积 h1_conv = nd.Convolution(data=X, weight=lenet_W1, bias=lenet_b1, kernel=lenet_W1.shape[2:], num_filter=lenet_W1.shape[0]) h1_activation = nd.relu(h1_conv) h1 = nd.Pooling(data=h1_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) # 第二层卷积 h2_conv = nd.Convolution(data=h1, weight=lenet_W2, bias=lenet_b2, kernel=lenet_W2.shape[2:], num_filter=lenet_W2.shape[0]) h2_activation = nd.relu(h2_conv) h2 = nd.Pooling(data=h2_activation, pool_type="max", kernel=(2, 2), stride=(2, 2)) h2 = nd.flatten(h2) # 第一层全连接 h3_linear = nd.dot(h2, lenet_W3) + lenet_b3 h3 = nd.relu(h3_linear) # 第二层全连接 h4_linear = nd.dot(h3, lenet_W4) + lenet_b4 if verbose: print('1st conv block:', h1.shape) print('2nd conv block:', h2.shape) print('1st dense:', h3.shape) print('2nd dense:', h4_linear.shape) print('output:', h4_linear) return h4_linear
def train_net(args): ctx = [] cvd = os.environ['CUDA_VISIBLE_DEVICES'].strip() if len(cvd) > 0: for i in xrange(len(cvd.split(','))): ctx.append(mx.gpu(i)) if len(ctx) == 0: ctx = [mx.cpu()] print('use cpu') else: print('gpu num:', len(ctx)) prefix = args.prefix prefix_dir = os.path.dirname(prefix) if not os.path.exists(prefix_dir): os.makedirs(prefix_dir) end_epoch = args.end_epoch args.ctx_num = len(ctx) args.num_layers = int(args.network[1:]) print('num_layers', args.num_layers) if args.per_batch_size == 0: args.per_batch_size = 128 args.batch_size = args.per_batch_size * args.ctx_num args.image_channel = 3 data_dir = args.data_dir if args.task == 'gender': data_dir = args.gender_data_dir elif args.task == 'age': data_dir = args.age_data_dir print('data dir', data_dir) path_imgrec = None path_imglist = None prop = face_image.load_property(data_dir) args.num_classes = prop.num_classes image_size = prop.image_size args.image_h = image_size[0] args.image_w = image_size[1] print('image_size', image_size) assert (args.num_classes > 0) print('num_classes', args.num_classes) path_imgrec = os.path.join(data_dir, "train.rec") print('Called with argument:', args) data_shape = (args.image_channel, image_size[0], image_size[1]) mean = None begin_epoch = 0 net = get_model() #if args.task=='': # test_net = get_model_test(net) #print(net.__class__) #net = net0[0] if args.network[0] == 'r' or args.network[0] == 'y': initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="out", magnitude=2) #resnet style elif args.network[0] == 'i' or args.network[0] == 'x': initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2) #inception else: initializer = mx.init.Xavier(rnd_type='uniform', factor_type="in", magnitude=2) net.hybridize() if args.mode == 'gluon': if len(args.pretrained) == 0: pass else: net.load_params(args.pretrained, allow_missing=True, ignore_extra=True) net.initialize(initializer) net.collect_params().reset_ctx(ctx) val_iter = None if args.task == '': train_iter = FaceImageIter( batch_size=args.batch_size, data_shape=data_shape, path_imgrec=path_imgrec, shuffle=True, rand_mirror=args.rand_mirror, mean=mean, cutoff=args.cutoff, ) else: train_iter = FaceImageIterAge( batch_size=args.batch_size, data_shape=data_shape, path_imgrec=path_imgrec, task=args.task, shuffle=True, rand_mirror=args.rand_mirror, mean=mean, cutoff=args.cutoff, ) if args.task == 'age': metric = CompositeEvalMetric([MAEMetric(), CUMMetric()]) elif args.task == 'gender': metric = CompositeEvalMetric([AccMetric()]) else: metric = CompositeEvalMetric([AccMetric()]) ver_list = [] ver_name_list = [] if args.task == '': for name in args.eval.split(','): path = os.path.join(data_dir, name + ".bin") if os.path.exists(path): data_set = verification.load_bin(path, image_size) ver_list.append(data_set) ver_name_list.append(name) print('ver', name) def ver_test(nbatch): results = [] for i in xrange(len(ver_list)): acc1, std1, acc2, std2, xnorm, embeddings_list = verification.test( ver_list[i], net, ctx, batch_size=args.batch_size) print('[%s][%d]XNorm: %f' % (ver_name_list[i], nbatch, xnorm)) #print('[%s][%d]Accuracy: %1.5f+-%1.5f' % (ver_name_list[i], nbatch, acc1, std1)) print('[%s][%d]Accuracy-Flip: %1.5f+-%1.5f' % (ver_name_list[i], nbatch, acc2, std2)) results.append(acc2) return results def val_test(nbatch=0): acc = 0.0 #if args.task=='age': if len(args.age_data_dir) > 0: val_iter = FaceImageIterAge( batch_size=args.batch_size, data_shape=data_shape, path_imgrec=os.path.join(args.age_data_dir, 'val.rec'), task=args.task, shuffle=False, rand_mirror=False, mean=mean, ) _metric = MAEMetric() val_metric = mx.metric.create(_metric) val_metric.reset() _metric2 = CUMMetric() val_metric2 = mx.metric.create(_metric2) val_metric2.reset() val_iter.reset() for batch in val_iter: data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] for x in data: outputs.append(net(x)[2]) val_metric.update(label, outputs) val_metric2.update(label, outputs) _value = val_metric.get_name_value()[0][1] print('[%d][VMAE]: %f' % (nbatch, _value)) _value = val_metric2.get_name_value()[0][1] if args.task == 'age': acc = _value print('[%d][VCUM]: %f' % (nbatch, _value)) if len(args.gender_data_dir) > 0: val_iter = FaceImageIterAge( batch_size=args.batch_size, data_shape=data_shape, path_imgrec=os.path.join(args.gender_data_dir, 'val.rec'), task=args.task, shuffle=False, rand_mirror=False, mean=mean, ) _metric = AccMetric() val_metric = mx.metric.create(_metric) val_metric.reset() val_iter.reset() for batch in val_iter: data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] for x in data: outputs.append(net(x)[1]) val_metric.update(label, outputs) _value = val_metric.get_name_value()[0][1] if args.task == 'gender': acc = _value print('[%d][VACC]: %f' % (nbatch, _value)) return acc total_time = 0 num_epochs = 0 best_acc = [0] highest_acc = [0.0, 0.0] #lfw and target global_step = [0] save_step = [0] if len(args.lr_steps) == 0: lr_steps = [100000, 140000, 160000] p = 512.0 / args.batch_size for l in xrange(len(lr_steps)): lr_steps[l] = int(lr_steps[l] * p) else: lr_steps = [int(x) for x in args.lr_steps.split(',')] print('lr_steps', lr_steps) kv = mx.kv.create('device') #kv = mx.kv.create('local') #_rescale = 1.0/args.ctx_num #opt = optimizer.SGD(learning_rate=args.lr, momentum=args.mom, wd=args.wd, rescale_grad=_rescale) #opt = optimizer.SGD(learning_rate=args.lr, momentum=args.mom, wd=args.wd) if args.mode == 'gluon': trainer = gluon.Trainer(net.collect_params(), 'sgd', { 'learning_rate': args.lr, 'wd': args.wd, 'momentum': args.mom, 'multi_precision': True }, kvstore=kv) else: _rescale = 1.0 / args.ctx_num opt = optimizer.SGD(learning_rate=args.lr, momentum=args.mom, wd=args.wd, rescale_grad=_rescale) _cb = mx.callback.Speedometer(args.batch_size, 20) arg_params = None aux_params = None data = mx.sym.var('data') label = mx.sym.var('softmax_label') if args.margin_a > 0.0: fc7 = net(data, label) else: fc7 = net(data) #sym = mx.symbol.SoftmaxOutput(data=fc7, label = label, name='softmax', normalization='valid') ceop = gluon.loss.SoftmaxCrossEntropyLoss() loss = ceop(fc7, label) #loss = loss/args.per_batch_size loss = mx.sym.mean(loss) sym = mx.sym.Group([ mx.symbol.BlockGrad(fc7), mx.symbol.MakeLoss(loss, name='softmax') ]) def _batch_callback(): mbatch = global_step[0] global_step[0] += 1 for _lr in lr_steps: if mbatch == _lr: args.lr *= 0.1 if args.mode == 'gluon': trainer.set_learning_rate(args.lr) else: opt.lr = args.lr print('lr change to', args.lr) break #_cb(param) if mbatch % 1000 == 0: print('lr-batch-epoch:', args.lr, mbatch) if mbatch > 0 and mbatch % args.verbose == 0: save_step[0] += 1 msave = save_step[0] do_save = False is_highest = False if args.task == 'age' or args.task == 'gender': acc = val_test(mbatch) if acc >= highest_acc[-1]: highest_acc[-1] = acc is_highest = True do_save = True else: acc_list = ver_test(mbatch) if len(acc_list) > 0: lfw_score = acc_list[0] if lfw_score > highest_acc[0]: highest_acc[0] = lfw_score if lfw_score >= 0.998: do_save = True if acc_list[-1] >= highest_acc[-1]: highest_acc[-1] = acc_list[-1] if lfw_score >= 0.99: do_save = True is_highest = True if args.ckpt == 0: do_save = False elif args.ckpt > 1: do_save = True if do_save: print('saving', msave) #print('saving gluon params') fname = os.path.join(args.prefix, 'model-gluon.params') net.save_params(fname) fname = os.path.join(args.prefix, 'model') net.export(fname, msave) #arg, aux = model.get_params() #mx.model.save_checkpoint(prefix, msave, model.symbol, arg, aux) print('[%d]Accuracy-Highest: %1.5f' % (mbatch, highest_acc[-1])) if args.max_steps > 0 and mbatch > args.max_steps: sys.exit(0) def _batch_callback_sym(param): _cb(param) _batch_callback() if args.mode != 'gluon': model = mx.mod.Module( context=ctx, symbol=sym, ) model.fit(train_iter, begin_epoch=0, num_epoch=args.end_epoch, eval_data=None, eval_metric=metric, kvstore='device', optimizer=opt, initializer=initializer, arg_params=arg_params, aux_params=aux_params, allow_missing=True, batch_end_callback=_batch_callback_sym, epoch_end_callback=None) else: loss_weight = 1.0 if args.task == 'age': loss_weight = 1.0 / AGE #loss = gluon.loss.SoftmaxCrossEntropyLoss(weight = loss_weight) loss = nd.SoftmaxOutput #loss = gluon.loss.SoftmaxCrossEntropyLoss() while True: #trainer = update_learning_rate(opt.lr, trainer, epoch, opt.lr_factor, lr_steps) tic = time.time() train_iter.reset() metric.reset() btic = time.time() for i, batch in enumerate(train_iter): _batch_callback() #data = gluon.utils.split_and_load(batch.data[0].astype(opt.dtype), ctx_list=ctx, batch_axis=0) #label = gluon.utils.split_and_load(batch.label[0].astype(opt.dtype), ctx_list=ctx, batch_axis=0) data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] Ls = [] with ag.record(): for x, y in zip(data, label): #print(y.asnumpy()) if args.task == '': if args.margin_a > 0.0: z = net(x, y) else: z = net(x) #print(z[0].shape, z[1].shape) else: z = net(x) if args.task == 'gender': L = loss(z[1], y) #L = L/args.per_batch_size Ls.append(L) outputs.append(z[1]) elif args.task == 'age': for k in xrange(AGE): _z = nd.slice_axis(z[2], axis=1, begin=k * 2, end=k * 2 + 2) _y = nd.slice_axis(y, axis=1, begin=k, end=k + 1) _y = nd.flatten(_y) L = loss(_z, _y) #L = L/args.per_batch_size #L /= AGE Ls.append(L) outputs.append(z[2]) else: L = loss(z, y) #L = L/args.per_batch_size Ls.append(L) outputs.append(z) # store the loss and do backward after we have done forward # on all GPUs for better speed on multiple GPUs. ag.backward(Ls) #trainer.step(batch.data[0].shape[0], ignore_stale_grad=True) #trainer.step(args.ctx_num) n = batch.data[0].shape[0] #print(n,n) trainer.step(n) metric.update(label, outputs) if i > 0 and i % 20 == 0: name, acc = metric.get() if len(name) == 2: logger.info( 'Epoch[%d] Batch [%d]\tSpeed: %f samples/sec\t%s=%f, %s=%f' % (num_epochs, i, args.batch_size / (time.time() - btic), name[0], acc[0], name[1], acc[1])) else: logger.info( 'Epoch[%d] Batch [%d]\tSpeed: %f samples/sec\t%s=%f' % (num_epochs, i, args.batch_size / (time.time() - btic), name[0], acc[0])) #metric.reset() btic = time.time() epoch_time = time.time() - tic # First epoch will usually be much slower than the subsequent epics, # so don't factor into the average if num_epochs > 0: total_time = total_time + epoch_time #name, acc = metric.get() #logger.info('[Epoch %d] training: %s=%f, %s=%f'%(num_epochs, name[0], acc[0], name[1], acc[1])) logger.info('[Epoch %d] time cost: %f' % (num_epochs, epoch_time)) num_epochs = num_epochs + 1 #name, val_acc = test(ctx, val_data) #logger.info('[Epoch %d] validation: %s=%f, %s=%f'%(epoch, name[0], val_acc[0], name[1], val_acc[1])) # save model if meet requirements #save_checkpoint(epoch, val_acc[0], best_acc) if num_epochs > 1: print('Average epoch time: {}'.format( float(total_time) / (num_epochs - 1)))
def getpred(from_layers, num_classes, sizes, ratios, mode='arm', verbose=False): ''' function: According to calc conv, we can predict results of ssd layer ,which include ARM and ODM . layers: outputs of network (ssd_layers and odm_layers) ''' loc_layers = [] cls_layers = [] anchor_layers = [] num_classes += 1 # add background for k, from_layer in enumerate(from_layers): # TODO: Add intermediate layers if it is necessary # estimate number of anchors per location size = sizes[k] assert len(size) > 0, 'must provide at least one size' ratio = ratios[k] assert len(ratio) > 0, 'must provide at least one ratio' size_str = '(' + ','.join([str(x) for x in ratio]) + ')' # (ratio) num_anchors = len(size) + len( ratio) - 1 # layers[0]: 4, layers[:]: 6 per pixel # 1. create location prediction for layers num_loc_pred = num_anchors * 4 # bbox param loc_pred = box_predictor(from_layer, num_loc_pred, k, verbose=verbose) ## transpose(0,2,3,1).flatten() ## TODO: Memory clear ? loc_pred = nd.transpose(loc_pred, axes=(0, 2, 3, 1)) loc_pred = nd.flatten(loc_pred) # (batch,h*w*num_loc_pred) loc_layers.append(loc_pred) # 2. create class prediction layer num_cls_pred = num_anchors * num_classes cls_pred = class_predictor(from_layer, num_cls_pred, k, verbose=verbose) cls_pred = nd.transpose(cls_pred, axes=(0, 2, 3, 1)) cls_pred = nd.flatten(cls_pred) # (batch,h*w*num_cls_pred) cls_layers.append(cls_pred) # 3. generate anchors if mode == 'arm': # anchor shape: (1,h*w*num_anchors,4) anchors = MultiBoxPrior(from_layer, size, ratio) if verbose: print('arm_layer_{}_anchors: {}'.format(k, anchors.shape)) anchor_layers.append(anchors) # Be calfull anchors' shape # 4. concat all param loc_preds = nd.concat(*loc_layers, dim=1) #(batch,h*w*num_loc_pred[:layers]) cls_preds = nd.concat(*cls_layers, dim=1).reshape( (0, -1, num_classes)) #(batch, h*w*num_anchors[:layers], num_classes) cls_preds = nd.transpose( cls_preds, axes=(0, 2, 1)) #(batch, num_classes, h*w*num_anchors[:layers] ) if mode == 'arm': anchor_boxes = nd.concat(*anchor_layers, dim=1) # (1,h*w*num_anchors[:layers],4) # print('arm_loc_preds : {}, arm_cls_pred : {}, arm_anchors {}'.format(loc_preds.shape,cls_preds.shape,anchor_boxes.shape)) return [ loc_preds.as_in_context(mx.gpu(0)), cls_preds.as_in_context(mx.gpu(0)), anchor_boxes.as_in_context(mx.gpu(0)) ] else: # print('odm_loc_preds : {}, odm_cls_pred : {} '.format(loc_preds.shape,cls_preds.shape)) return [ loc_preds.as_in_context(mx.gpu(0)), cls_preds.as_in_context(mx.gpu(0)) ]
def forward(self, x): inp = x.shape[1] x = nd.Pooling(x, global_pool=True) x = nd.flatten(x) x = nd.dot(x, self.weight.data()[:inp,:]) + self.bias.data() return x
def net(X, params, debug=False, pool_type='avg', pool_size=16, pool_stride=2, act_type='relu', dilate_size=1, nf=1): [W1, b1, W2, b2, W3, b3, W4, b4, W5, b5] = params ######################## # Define the computation of the first convolutional layer ######################## h1_conv = nd.Convolution(data=X, weight=W1, bias=b1, kernel=(1, 16), num_filter=int(16 * nf), stride=(1, 1), dilate=(1, dilate_size)) h1_activation = activation(h1_conv, act_type=act_type) h1 = nd.Pooling(data=h1_activation, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) if debug: print("h1 shape: %s" % (np.array(h1.shape))) ######################## # Define the computation of the second convolutional layer ######################## h2_conv = nd.Convolution(data=h1, weight=W2, bias=b2, kernel=(1, 8), num_filter=int(32 * nf), stride=(1, 1), dilate=(1, dilate_size)) h2_activation = activation(h2_conv, act_type=act_type) h2 = nd.Pooling(data=h2_activation, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) if debug: print("h2 shape: %s" % (np.array(h2.shape))) ######################## # Define the computation of the third convolutional layer ######################## h3_conv = nd.Convolution(data=h2, weight=W3, bias=b3, kernel=(1, 8), num_filter=int(64 * nf), stride=(1, 1), dilate=(1, dilate_size)) h3_activation = activation(h3_conv, act_type=act_type) h3 = nd.Pooling(data=h3_activation, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) if debug: print("h3 shape: %s" % (np.array(h3.shape))) ######################## # Flattening h3 so that we can feed it into a fully-connected layer ######################## h4 = nd.flatten(h3) if debug: print("Flat h4 shape: %s" % (np.array(h4.shape))) ######################## # Define the computation of the 4th (fully-connected) layer ######################## h5_linear = nd.dot(h4, W4) + b4 h5 = activation(h5_linear, act_type=act_type) if autograd.is_training(): # 对激活函数的输出使用droupout h5 = dropout(h5, drop_prob) if debug: print("h5 shape: %s" % (np.array(h5.shape))) print("Dropout: ", drop_prob) ######################## # Define the computation of the output layer ######################## yhat_linear = nd.dot(h5, W5) + b5 if debug: print("yhat_linear shape: %s" % (np.array(yhat_linear.shape))) interlayer = [h1, h2, h3, h4, h5] return yhat_linear, interlayer
def net_PRL(X, params, debug=False, pool_type='max', pool_size=4, pool_stride=2): [W1, b1, W2, b2, W3, b3, W4, b4, W5, b5, W6, b6, W7, b7, W8, b8, W9, b9] = params drop_prob = 0.5 ######################## # Define the computation of the first convolutional layer ######################## h1_conv = nd.Convolution(data=X, weight=W1, bias=b1, kernel=(1, 64), num_filter=8, stride=(1, 1), dilate=(1, 1)) h1 = nd.LeakyReLU(h1_conv, act_type='elu') if debug: print("h1 shape: %s" % (np.array(h1.shape))) ######################## # Define the computation of the second convolutional layer ######################## h2_conv = nd.Convolution(data=h1, weight=W2, bias=b2, kernel=(1, 32), num_filter=8, stride=(1, 1), dilate=(1, 1)) h2_pooling = nd.Pooling(data=h2_conv, pool_type=pool_type, kernel=(1, 8), stride=(1, pool_stride)) h2 = nd.LeakyReLU(h2_pooling, act_type='elu') if debug: print("h2 shape: %s" % (np.array(h2.shape))) ######################## # Define the computation of the third convolutional layer ######################## h3_conv = nd.Convolution(data=h2, weight=W3, bias=b3, kernel=(1, 32), num_filter=16, stride=(1, 1), dilate=(1, 1)) h3 = nd.LeakyReLU(h3_conv, act_type='elu') if debug: print("h3 shape: %s" % (np.array(h3.shape))) ######################## # Define the computation of the 4th convolutional layer ######################## h4_conv = nd.Convolution(data=h3, weight=W4, bias=b4, kernel=(1, 16), num_filter=16, stride=(1, 1), dilate=(1, 1)) h4_pooling = nd.Pooling(data=h4_conv, pool_type=pool_type, kernel=(1, 6), stride=(1, pool_stride)) h4 = nd.LeakyReLU(h4_pooling, act_type='elu') if debug: print("h4 shape: %s" % (np.array(h4.shape))) ######################## # Define the computation of the 5th convolutional layer ######################## h5_conv = nd.Convolution(data=h4, weight=W5, bias=b5, kernel=(1, 16), num_filter=32, stride=(1, 1), dilate=(1, 1)) h5 = nd.LeakyReLU(h5_conv, act_type='elu') if debug: print("h5 shape: %s" % (np.array(h5.shape))) ######################## # Define the computation of the 6th convolutional layer ######################## h6_conv = nd.Convolution(data=h5, weight=W6, bias=b6, kernel=(1, 16), num_filter=32, stride=(1, 1), dilate=(1, 1)) h6_pooling = nd.Pooling(data=h6_conv, pool_type=pool_type, kernel=(1, 4), stride=(1, pool_stride)) h6 = nd.LeakyReLU(h6_pooling, act_type='elu') if debug: print("h6 shape: %s" % (np.array(h6.shape))) ######################## # Flattening h6 so that we can feed it into a fully-connected layer ######################## h7 = nd.flatten(h6) if debug: print("Flat h7 shape: %s" % (np.array(h7.shape))) ######################## # Define the computation of the 8th (fully-connected) layer ######################## h8_linear = nd.dot(h7, W7) + b7 h8 = nd.LeakyReLU(h8_linear, act_type='elu') if autograd.is_training(): # 对激活函数的输出使用droupout h8 = dropout(h8, drop_prob) if debug: print("h8 shape: %s" % (np.array(h8.shape))) ######################## # Define the computation of the 9th (fully-connected) layer ######################## h9_linear = nd.dot(h8, W8) + b8 h9 = nd.LeakyReLU(h9_linear, act_type='elu') if autograd.is_training(): # 对激活函数的输出使用droupout h9 = dropout(h9, drop_prob) if debug: print("h9 shape: %s" % (np.array(h9.shape))) ######################## # Define the computation of the output layer ######################## yhat_linear = nd.dot(h9, W9) + b9 if debug: print("yhat_linear shape: %s" % (np.array(yhat_linear.shape))) interlayer = [ W1, b1, W2, b2, W3, b3, W4, b4, W5, b5, W6, b6, W7, b7, W8, b8, W9, b9 ] return yhat_linear, interlayer
def flatten_preds(preds): return nd.flatten(nd.transpose(preds, axes=(0, 2, 3, 1)))
def net_PLB(X, params, debug=False, pool_type='max', pool_size=4, pool_stride=4): [W1, b1, W2, b2, W3, b3, W4, b4, W5, b5, W6, b6, W7, b7] = params ######################## # Define the computation of the first convolutional layer ######################## h1_conv = nd.Convolution(data=X, weight=W1, bias=b1, kernel=(1, 16), num_filter=64, stride=(1, 1), dilate=(1, 1)) h1_pooling = nd.Pooling(data=h1_conv, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) h1 = relu(h1_pooling) if debug: print("h1 shape: %s" % (np.array(h1.shape))) ######################## # Define the computation of the second convolutional layer ######################## h2_conv = nd.Convolution(data=h1, weight=W2, bias=b2, kernel=(1, 16), num_filter=128, stride=(1, 1), dilate=(1, 2)) h2_pooling = nd.Pooling(data=h2_conv, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) h2 = relu(h2_pooling) if debug: print("h2 shape: %s" % (np.array(h2.shape))) ######################## # Define the computation of the third convolutional layer ######################## h3_conv = nd.Convolution(data=h2, weight=W3, bias=b3, kernel=(1, 16), num_filter=256, stride=(1, 1), dilate=(1, 2)) h3_pooling = nd.Pooling(data=h3_conv, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) h3 = relu(h3_pooling) if debug: print("h3 shape: %s" % (np.array(h3.shape))) ######################## # Define the computation of the 4th convolutional layer ######################## h4_conv = nd.Convolution(data=h3, weight=W4, bias=b4, kernel=(1, 32), num_filter=512, stride=(1, 1), dilate=(1, 2)) h4_pooling = nd.Pooling(data=h4_conv, pool_type=pool_type, kernel=(1, pool_size), stride=(1, pool_stride)) h4 = relu(h4_pooling) if debug: print("h4 shape: %s" % (np.array(h4.shape))) ######################## # Flattening h4 so that we can feed it into a fully-connected layer ######################## h5 = nd.flatten(h4) if debug: print("Flat h5 shape: %s" % (np.array(h5.shape))) ######################## # Define the computation of the 5th (fully-connected) layer ######################## h6_linear = nd.dot(h5, W5) + b5 h6 = relu(h6_linear) if debug: print("h6 shape: %s" % (np.array(h6.shape))) ######################## # Define the computation of the 6th (fully-connected) layer ######################## h7_linear = nd.dot(h6, W6) + b6 h7 = relu(h7_linear) if debug: print("h7 shape: %s" % (np.array(h7.shape))) ######################## # Define the computation of the output layer ######################## yhat_linear = nd.dot(h7, W7) + b7 if debug: print("yhat_linear shape: %s" % (np.array(yhat_linear.shape))) interlayer = [W1, b1, W2, b2, W3, b3, W4, b4, W5, b5, W6, b6, W7, b7] return yhat_linear, interlayer
feature_vector_list = [] # Maintains each obtained feature vector # ----------- Extraction Loop ---------- for epoch in range(1): print("\n >>>> Processing Images") input_iterator.reset() all_labels = [] for batch_id, batch in enumerate(input_iterator): data = batch.data[0].as_in_context(ctx) # Fetch batch image data labels = batch.label[0].as_in_context(ctx) # Fetch ground truth labels out = net(data) output = nd.flatten(out).asnumpy().tolist() # Obtain features all_labels.extend(labels.asnumpy()) # Extend label list for single_output_item in output: # Append output features to feature list feature_vector_list.append(single_output_item) # Display check if batch_id % 5 == 0: print(" >> Images Processed: " + str(batch_id * batch_size)) # ----------<Write Features to File >---------- write_features_to_file(feature_vector_list, all_labels, feature_file) print("\n\n >>>>>> Thanks for Extracting Features\n\n")
def network( X, drop_rate=0.0 ): # formula : output_size=((input−weights+2*Padding)/Stride)+1 #data size # MNIST,FashionMNIST = (batch size , 1 , 28 , 28) # CIFAR = (batch size , 3 , 32 , 32) # builtin The BatchNorm function moving_mean, moving_var does not work. C_H1 = nd.Activation( data=nd.BatchNorm(data=nd.Convolution(data=X, weight=W1, bias=B1, kernel=(3, 3), stride=(1, 1), num_filter=60), gamma=gamma1, beta=beta1, moving_mean=ma1, moving_var=mv1, momentum=0.9, fix_gamma=False, use_global_stats=True), act_type="relu" ) # MNIST : result = ( batch size , 60 , 26 , 26) , CIFAR10 : : result = ( batch size , 60 , 30 , 30) P_H1 = nd.Pooling( data=C_H1, pool_type="avg", kernel=(2, 2), stride=(2, 2) ) # MNIST : result = (batch size , 60 , 13 , 13) , CIFAR10 : result = (batch size , 60 , 15 , 15) C_H2 = nd.Activation( data=nd.BatchNorm(data=nd.Convolution(data=P_H1, weight=W2, bias=B2, kernel=(6, 6), stride=(1, 1), num_filter=30), gamma=gamma2, beta=beta2, moving_mean=ma2, moving_var=mv2, momentum=0.9, fix_gamma=False, use_global_stats=True), act_type="relu" ) # MNIST : result = ( batch size , 30 , 8 , 8), CIFAR10 : result = ( batch size , 30 , 10 , 10) P_H2 = nd.Pooling( data=C_H2, pool_type="avg", kernel=(2, 2), stride=(2, 2) ) # MNIST : result = (batch size , 30 , 4 , 4) , CIFAR10 : result = (batch size , 30 , 5 , 5) P_H2 = nd.flatten(data=P_H2) '''FullyConnected parameter • data: (batch_size, input_dim) • weight: (num_hidden, input_dim) • bias: (num_hidden,) • out: (batch_size, num_hidden) ''' F_H1 = nd.Activation(nd.BatchNorm(data=nd.FullyConnected( data=P_H2, weight=W3, bias=B3, num_hidden=120), gamma=gamma3, beta=beta3, moving_mean=ma3, moving_var=mv3, momentum=0.9, fix_gamma=False, use_global_stats=True), act_type="relu") F_H1 = nd.Dropout(data=F_H1, p=drop_rate) F_H2 = nd.Activation(nd.BatchNorm(data=nd.FullyConnected( data=F_H1, weight=W4, bias=B4, num_hidden=64), gamma=gamma4, beta=beta4, moving_mean=ma4, moving_var=mv4, momentum=0.9, fix_gamma=False, use_global_stats=True), act_type="relu") F_H2 = nd.Dropout(data=F_H2, p=drop_rate) #softmax_Y = nd.softmax(nd.FullyConnected(data=F_H2 ,weight=W5 , bias=B5 , num_hidden=10)) out = nd.FullyConnected(data=F_H2, weight=W5, bias=B5, num_hidden=10) return out
def train_net(args): ctx = [] cvd = os.environ['CUDA_VISIBLE_DEVICES'].strip() if len(cvd)>0: for i in xrange(len(cvd.split(','))): ctx.append(mx.gpu(i)) if len(ctx)==0: ctx = [mx.cpu()] print('use cpu') else: print('gpu num:', len(ctx)) prefix = args.prefix prefix_dir = os.path.dirname(prefix) if not os.path.exists(prefix_dir): os.makedirs(prefix_dir) end_epoch = args.end_epoch args.ctx_num = len(ctx) args.num_layers = int(args.network[1:]) print('num_layers', args.num_layers) if args.per_batch_size==0: args.per_batch_size = 128 args.batch_size = args.per_batch_size*args.ctx_num args.image_channel = 3 data_dir = args.data_dir if args.task=='gender': data_dir = args.gender_data_dir elif args.task=='age': data_dir = args.age_data_dir print('data dir', data_dir) path_imgrec = None path_imglist = None prop = face_image.load_property(data_dir) args.num_classes = prop.num_classes image_size = prop.image_size args.image_h = image_size[0] args.image_w = image_size[1] print('image_size', image_size) assert(args.num_classes>0) print('num_classes', args.num_classes) path_imgrec = os.path.join(data_dir, "train.rec") print('Called with argument:', args) data_shape = (args.image_channel,image_size[0],image_size[1]) mean = None begin_epoch = 0 net = get_model() #if args.task=='': # test_net = get_model_test(net) #print(net.__class__) #net = net0[0] if args.network[0]=='r' or args.network[0]=='y': initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="out", magnitude=2) #resnet style elif args.network[0]=='i' or args.network[0]=='x': initializer = mx.init.Xavier(rnd_type='gaussian', factor_type="in", magnitude=2) #inception else: initializer = mx.init.Xavier(rnd_type='uniform', factor_type="in", magnitude=2) net.hybridize() if args.mode=='gluon': if len(args.pretrained)==0: pass else: net.load_params(args.pretrained, allow_missing=True, ignore_extra = True) net.initialize(initializer) net.collect_params().reset_ctx(ctx) val_iter = None if args.task=='': train_iter = FaceImageIter( batch_size = args.batch_size, data_shape = data_shape, path_imgrec = path_imgrec, shuffle = True, rand_mirror = args.rand_mirror, mean = mean, cutoff = args.cutoff, ) else: train_iter = FaceImageIterAge( batch_size = args.batch_size, data_shape = data_shape, path_imgrec = path_imgrec, task = args.task, shuffle = True, rand_mirror = args.rand_mirror, mean = mean, cutoff = args.cutoff, ) if args.task=='age': metric = CompositeEvalMetric([MAEMetric(), CUMMetric()]) elif args.task=='gender': metric = CompositeEvalMetric([AccMetric()]) else: metric = CompositeEvalMetric([AccMetric()]) ver_list = [] ver_name_list = [] if args.task=='': for name in args.eval.split(','): path = os.path.join(data_dir,name+".bin") if os.path.exists(path): data_set = verification.load_bin(path, image_size) ver_list.append(data_set) ver_name_list.append(name) print('ver', name) def ver_test(nbatch): results = [] for i in xrange(len(ver_list)): acc1, std1, acc2, std2, xnorm, embeddings_list = verification.test(ver_list[i], net, ctx, batch_size = args.batch_size) print('[%s][%d]XNorm: %f' % (ver_name_list[i], nbatch, xnorm)) #print('[%s][%d]Accuracy: %1.5f+-%1.5f' % (ver_name_list[i], nbatch, acc1, std1)) print('[%s][%d]Accuracy-Flip: %1.5f+-%1.5f' % (ver_name_list[i], nbatch, acc2, std2)) results.append(acc2) return results def val_test(nbatch=0): acc = 0.0 #if args.task=='age': if len(args.age_data_dir)>0: val_iter = FaceImageIterAge( batch_size = args.batch_size, data_shape = data_shape, path_imgrec = os.path.join(args.age_data_dir, 'val.rec'), task = args.task, shuffle = False, rand_mirror = False, mean = mean, ) _metric = MAEMetric() val_metric = mx.metric.create(_metric) val_metric.reset() _metric2 = CUMMetric() val_metric2 = mx.metric.create(_metric2) val_metric2.reset() val_iter.reset() for batch in val_iter: data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] for x in data: outputs.append(net(x)[2]) val_metric.update(label, outputs) val_metric2.update(label, outputs) _value = val_metric.get_name_value()[0][1] print('[%d][VMAE]: %f'%(nbatch, _value)) _value = val_metric2.get_name_value()[0][1] if args.task=='age': acc = _value print('[%d][VCUM]: %f'%(nbatch, _value)) if len(args.gender_data_dir)>0: val_iter = FaceImageIterAge( batch_size = args.batch_size, data_shape = data_shape, path_imgrec = os.path.join(args.gender_data_dir, 'val.rec'), task = args.task, shuffle = False, rand_mirror = False, mean = mean, ) _metric = AccMetric() val_metric = mx.metric.create(_metric) val_metric.reset() val_iter.reset() for batch in val_iter: data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] for x in data: outputs.append(net(x)[1]) val_metric.update(label, outputs) _value = val_metric.get_name_value()[0][1] if args.task=='gender': acc = _value print('[%d][VACC]: %f'%(nbatch, _value)) return acc total_time = 0 num_epochs = 0 best_acc = [0] highest_acc = [0.0, 0.0] #lfw and target global_step = [0] save_step = [0] if len(args.lr_steps)==0: lr_steps = [100000, 140000, 160000] p = 512.0/args.batch_size for l in xrange(len(lr_steps)): lr_steps[l] = int(lr_steps[l]*p) else: lr_steps = [int(x) for x in args.lr_steps.split(',')] print('lr_steps', lr_steps) kv = mx.kv.create('device') #kv = mx.kv.create('local') #_rescale = 1.0/args.ctx_num #opt = optimizer.SGD(learning_rate=args.lr, momentum=args.mom, wd=args.wd, rescale_grad=_rescale) #opt = optimizer.SGD(learning_rate=args.lr, momentum=args.mom, wd=args.wd) if args.mode=='gluon': trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': args.lr, 'wd': args.wd, 'momentum': args.mom, 'multi_precision': True}, kvstore=kv) else: _rescale = 1.0/args.ctx_num opt = optimizer.SGD(learning_rate=args.lr, momentum=args.mom, wd=args.wd, rescale_grad=_rescale) _cb = mx.callback.Speedometer(args.batch_size, 20) arg_params = None aux_params = None data = mx.sym.var('data') label = mx.sym.var('softmax_label') if args.margin_a>0.0: fc7 = net(data, label) else: fc7 = net(data) #sym = mx.symbol.SoftmaxOutput(data=fc7, label = label, name='softmax', normalization='valid') ceop = gluon.loss.SoftmaxCrossEntropyLoss() loss = ceop(fc7, label) #loss = loss/args.per_batch_size loss = mx.sym.mean(loss) sym = mx.sym.Group( [mx.symbol.BlockGrad(fc7), mx.symbol.MakeLoss(loss, name='softmax')] ) def _batch_callback(): mbatch = global_step[0] global_step[0]+=1 for _lr in lr_steps: if mbatch==_lr: args.lr *= 0.1 if args.mode=='gluon': trainer.set_learning_rate(args.lr) else: opt.lr = args.lr print('lr change to', args.lr) break #_cb(param) if mbatch%1000==0: print('lr-batch-epoch:',args.lr, mbatch) if mbatch>0 and mbatch%args.verbose==0: save_step[0]+=1 msave = save_step[0] do_save = False is_highest = False if args.task=='age' or args.task=='gender': acc = val_test(mbatch) if acc>=highest_acc[-1]: highest_acc[-1] = acc is_highest = True do_save = True else: acc_list = ver_test(mbatch) if len(acc_list)>0: lfw_score = acc_list[0] if lfw_score>highest_acc[0]: highest_acc[0] = lfw_score if lfw_score>=0.998: do_save = True if acc_list[-1]>=highest_acc[-1]: highest_acc[-1] = acc_list[-1] if lfw_score>=0.99: do_save = True is_highest = True if args.ckpt==0: do_save = False elif args.ckpt>1: do_save = True if do_save: print('saving', msave) #print('saving gluon params') fname = os.path.join(args.prefix, 'model-gluon.params') net.save_params(fname) fname = os.path.join(args.prefix, 'model') net.export(fname, msave) #arg, aux = model.get_params() #mx.model.save_checkpoint(prefix, msave, model.symbol, arg, aux) print('[%d]Accuracy-Highest: %1.5f'%(mbatch, highest_acc[-1])) if args.max_steps>0 and mbatch>args.max_steps: sys.exit(0) def _batch_callback_sym(param): _cb(param) _batch_callback() if args.mode!='gluon': model = mx.mod.Module( context = ctx, symbol = sym, ) model.fit(train_iter, begin_epoch = 0, num_epoch = args.end_epoch, eval_data = None, eval_metric = metric, kvstore = 'device', optimizer = opt, initializer = initializer, arg_params = arg_params, aux_params = aux_params, allow_missing = True, batch_end_callback = _batch_callback_sym, epoch_end_callback = None ) else: loss_weight = 1.0 if args.task=='age': loss_weight = 1.0/AGE #loss = gluon.loss.SoftmaxCrossEntropyLoss(weight = loss_weight) loss = nd.SoftmaxOutput #loss = gluon.loss.SoftmaxCrossEntropyLoss() while True: #trainer = update_learning_rate(opt.lr, trainer, epoch, opt.lr_factor, lr_steps) tic = time.time() train_iter.reset() metric.reset() btic = time.time() for i, batch in enumerate(train_iter): _batch_callback() #data = gluon.utils.split_and_load(batch.data[0].astype(opt.dtype), ctx_list=ctx, batch_axis=0) #label = gluon.utils.split_and_load(batch.label[0].astype(opt.dtype), ctx_list=ctx, batch_axis=0) data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] Ls = [] with ag.record(): for x, y in zip(data, label): #print(y.asnumpy()) if args.task=='': if args.margin_a>0.0: z = net(x,y) else: z = net(x) #print(z[0].shape, z[1].shape) else: z = net(x) if args.task=='gender': L = loss(z[1], y) #L = L/args.per_batch_size Ls.append(L) outputs.append(z[1]) elif args.task=='age': for k in xrange(AGE): _z = nd.slice_axis(z[2], axis=1, begin=k*2, end=k*2+2) _y = nd.slice_axis(y, axis=1, begin=k, end=k+1) _y = nd.flatten(_y) L = loss(_z, _y) #L = L/args.per_batch_size #L /= AGE Ls.append(L) outputs.append(z[2]) else: L = loss(z, y) #L = L/args.per_batch_size Ls.append(L) outputs.append(z) # store the loss and do backward after we have done forward # on all GPUs for better speed on multiple GPUs. ag.backward(Ls) #trainer.step(batch.data[0].shape[0], ignore_stale_grad=True) #trainer.step(args.ctx_num) n = batch.data[0].shape[0] #print(n,n) trainer.step(n) metric.update(label, outputs) if i>0 and i%20==0: name, acc = metric.get() if len(name)==2: logger.info('Epoch[%d] Batch [%d]\tSpeed: %f samples/sec\t%s=%f, %s=%f'%( num_epochs, i, args.batch_size/(time.time()-btic), name[0], acc[0], name[1], acc[1])) else: logger.info('Epoch[%d] Batch [%d]\tSpeed: %f samples/sec\t%s=%f'%( num_epochs, i, args.batch_size/(time.time()-btic), name[0], acc[0])) #metric.reset() btic = time.time() epoch_time = time.time()-tic # First epoch will usually be much slower than the subsequent epics, # so don't factor into the average if num_epochs > 0: total_time = total_time + epoch_time #name, acc = metric.get() #logger.info('[Epoch %d] training: %s=%f, %s=%f'%(num_epochs, name[0], acc[0], name[1], acc[1])) logger.info('[Epoch %d] time cost: %f'%(num_epochs, epoch_time)) num_epochs = num_epochs + 1 #name, val_acc = test(ctx, val_data) #logger.info('[Epoch %d] validation: %s=%f, %s=%f'%(epoch, name[0], val_acc[0], name[1], val_acc[1])) # save model if meet requirements #save_checkpoint(epoch, val_acc[0], best_acc) if num_epochs > 1: print('Average epoch time: {}'.format(float(total_time)/(num_epochs - 1)))
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ check_label_shapes(labels, preds) for label, pred in zip(labels, preds): # start_all = time.time() pred = pred.reshape((pred.shape[0], pred.shape[1], -1)) # print label.shape, pred.shape if pred.shape != label.shape: # start = time.time() if self.threshold == 0.5 and self.num_class <= 2: pred = ndarray.argmax(pred, axis=self.axis) else: pred = pred[:, self.c, :] > self.threshold # end = time.time() # print 'bb2: %f' % (end - start) # print pred # check_label_shapes(label, pred) if label.shape != pred.shape: raise ValueError("Shape of labels {} does not match shape of " "predictions {}".format( label.shape, pred.shape)) # print label.shape # print pred_label.shape # end = time.time() # print 'bb: %f' % (end - start) if self.c is not None: pred = pred.copyto(mx.cpu(0)) # print pred.context # print label.context pred = ndarray.flatten(pred) label = ndarray.flatten(label) # print pred.shape, label.shape p_1 = pred == 1 p_0 = pred == 0 l_1 = label == 1 l_0 = label == 0 # quit() TP = ndarray.sum((p_1 * l_1), axis=[0, 1]) FP = ndarray.sum(p_1 * l_0, axis=[0, 1]) FN = ndarray.sum(p_0 * l_1, axis=[0, 1]) # start = time.time() self.TP += TP self.FP += FP self.FN += FN # print TP,FP,FN # print self.TP, self.FP, self.FN # self.TP += TP.asnumpy()[0] # self.FP += FP.asnumpy()[0] # self.FN += FN.asnumpy()[0] # end = time.time() # print 'cc2: %f' % (end - start) # quit() # print 'cc1: %f' % (end - start) # region 效率太低 # start = time.time() # pred = pred.asnumpy().astype('int32') # label = label.asnumpy().astype('int32') # pred_label_ = pred.flat # label_ = label.flat # for i in range(len(pred_label_)): # if label_[i] == self.c: # if pred_label_[i] == self.c: # self.TP += 1 # else: # self.FN += 1 # elif pred_label_[i] == self.c: # self.FP += 1 # print self.TP, self.FP, self.FN # self.MeanIoU.append(float(self.TP) / (self.TP + self.FN + self.FP)) # endregion else: # todo:这部分代码有问题 pass
def mxnet_flatten(in1, in2): return [mxnd.flatten(t) for t in in1], [mxnd.flatten(t) for t in in2]
def hybrid_forward(self, F, pred, label,sample_weight=None): loss = F.sqrt(F.square(F.flatten(pred)-F.flatten(label))) loss = loss.reshape(loss.shape[0],pred.shape[1],pred.shape[2]) return F.mean(loss,axis=1)