def save_checkpoint(self, name): ckp_path = os.path.join(self.model_dir, name) tmp = { 'network': self.net, 'opt_state': self.train_func.optimizer_state.make_checkpoint(), 'clock': self.clock.make_checkpoint(), } io.dump(tmp, ckp_path)
nonlinearity=mgsk.opr.helper.elemwise_trans.Identity()) f = Softmax("cls_softmax", f) f.init_weights() net = RawNetworkBuilder(inputs=[data], outputs=[f]) return net if __name__ == '__main__': parser = argparse.ArgumentParser( description='dump pkl model for resnet50', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-b', '--batch-size', help='batch size of the model', default=1) parser.add_argument('-f', '--format', choices=['NCHW', 'NCHW4'], help='format of conv', default='NCHW') parser.add_argument('-o', '--output', help='output pkl path', required=True) args = parser.parse_args() if args.format != 'NCHW': print('Only suppprt NCHW for float model') parser.print_help() sys.exit(1) from meghair.utils import io io.dump(get(args), args.output)
def R3(self): print("entered R3!!!!") oprs_dict = self.oprs_dict new_oprs_dict = self.new_oprs_dict DEBUG = True convs = self.convs end = 5 speed_ratio = 4 alldic = ['conv%d_1' % i for i in range(1, end) ] + ['conv%d_2' % i for i in range(3, end)] pooldic = { 'conv1_2': 'pool1', 'conv2_2': 'pool2', 'conv3_3': 'pool3', 'conv4_3': 'pool4' } rankdic = { 'conv1_1': 24, 'conv1_2': 22, 'conv2_1': 41, 'conv2_2': 51, 'conv3_1': 108, 'conv3_2': 89, 'conv3_3': 111, 'conv4_1': 184, 'conv4_2': 276, 'conv4_3': 228, 'conv5_1': 512, 'conv5_2': 512, 'conv5_3': 512 } for i in rankdic: if 'conv5' in i: continue # the break-statemet was giving a bug, so changed it to continue-statement -by Mario rankdic[i] = int(rankdic[i] * 4. / speed_ratio) c_ratio = 1.15 t = Timer() for conv, convnext in zip( convs[0:], convs[1:] + ['pool5'] ): # note that we exclude the first conv, conv1_1 contributes little computation -by Mario #W_shape = self.param_shape(conv) #d_c = int(W_shape[0] / c_ratio) #rank = rankdic[conv] #d_prime = rank #if d_c < rank: d_c = rank d_c = rankdic[conv] print("channel pruning", conv, convnext) '''channel pruning''' if (conv in alldic or conv in pooldic) and (convnext in self.convs): t.tic() if conv in pooldic: X_name = pooldic[conv] else: X_name = conv print(X_name, convnext) idxs, W2, B2 = self.dictionary_kernel(X_name, d_c, convnext) # W2 W_new = self.param_data(convnext) W_new[:, ~idxs, ...] = 0 W_new[:, idxs, ...] = W2.copy() self.new_oprs_dict[convnext + ':W'].set_value(W_new) self.new_oprs_dict[convnext + ':b'].set_value(B2) t.toc('channel_pruning') print("channel pruning finished") io.dump(self.new_net, 'outputs/' + X_name + 'pruned_model.save') self.val() io.dump(self.new_net, 'pruned_model.save')
def make_network(model, isize): data = [ O.DataProvider('input{}'.format(i), shape=isizes[i]) for i in range(len(isizes)) ] f = open(model, 'rb') engine = f.read() opr = TensorRTRuntimeOpr(data, engine, 1) net = RawNetworkBuilder(inputs=[data], outputs=opr.outputs) return net if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument(dest='model') parser.add_argument(dest='output') parser.add_argument( '--isize', help='input sizes. ' 'e.g. for models with two (1,3,224,224) inputs, ' 'the option --isize="1,3,224,224;1,3,224,224" should be used') args = parser.parse_args() isizes = [str2tuple(x) for x in args.isize.split(';')] net = make_network(args.model, isizes) dump(net, args.output)
func_test = Function().compile(network.pred) import pickle import gzip import numpy as np train_set, valid_set, test_set = pickle.load(gzip.open("mnist.pkl.gz", "rb"), encoding="latin1") minibatch_size = network.minibatch_size l = len(train_set[0]) epoch = 0 for i in range(100000): j = i % (l // minibatch_size) minibatch = train_set[0][j * minibatch_size:(j + 1) * minibatch_size] label = train_set[1][j * minibatch_size:(j + 1) * minibatch_size] minibatch = np.array(minibatch).reshape(-1, 1, 28, 28) loss = func(input_mat=minibatch, label=label) if j == 0: print("*****") print("epoch = ", epoch) epoch += 1 print("loss = ", loss) res = func_test( input_mat=np.array(valid_set[0]).reshape(-1, 1, 28, 28)) res = np.argmax(np.array(res), axis=1) acc = (np.array(res) == np.array(valid_set[1])).mean() print("acc = ", acc) dump(network.network, open("1.data", "wb"))