def targetClassify(model_name,input_pic,target_class): # The purpose of this is to simply output the percent probability that # the image is specified target_class # Load specified Model # Assume pretrained net = get_model(model_name, pretrained=True) classes = net.classes classInd = -1; # Find index of target class for i,j in enumerate(classes): if target_class == j.lower(): classInd = i break # Exit if target class not found if classInd == -1: print("ERROR: Target class not found in this model : %s" % target_class) return # Load Images, assume all data is in "images/" directory img = image.imread("images/" + input_pic) # Transform and predict img = transform_eval(img) pred = net(img) # use softmax and print probability #prob = nd.softmax(pred) print("Probability of class [%s] for [%s]: %.3f" % (classes[classInd],input_pic,nd.softmax(pred)[0][classInd].asscalar()))
def predict(images, model_name, model_config): """ Runs the model to infer on the given image Arguments: image {NDArray Image} -- image to do prediction on model_name {str} -- name of the model to be used for prediction Returns: dict -- dictionary containing the predicted class """ outputs = {} net = model_config.loaded_models[model_name] # apply default data preprocessing transformed_img = transform_eval(images) if(model_config.models_config[model_name]== 'gpu'): transformed_img = transformed_img.copyto(mx.gpu(0)) # run forward pass to obtain the predicted score for each class pred = net(transformed_img) # map predicted values to probability by softmax prob = nd.softmax(pred)[0].asnumpy() #prob = pred[0].asnumpy() with open(os.path.join("/models",str(model_name),'config.json')) as config_file: data = json.load(config_file) max_number_of_predictions = data['max_number_of_predictions'] minimum_confidence = data['minimum_confidence'] if(max_number_of_predictions>len(net.classes)): max_number_of_predictions=len(net.classes) if(max_number_of_predictions<1): max_number_of_predictions=1 # find the 5 class indices with the highest score ind = nd.topk(pred, k=max_number_of_predictions)[0].astype('int').asnumpy().tolist() if(minimum_confidence>float(prob[ind[0]])): minimum_confidence=float(prob[ind[-1]]) for i in range(max_number_of_predictions): if(float(prob[ind[i]])>minimum_confidence): outputs[net.classes[ind[i]]] = float(prob[ind[i]]) outputs_descending = OrderedDict(sorted(outputs.items(), key=lambda kv: kv[1], reverse=True)) return outputs_descending
def extract_features(model, path): ''' Extract embeddings from video. model (string): Name of pretrained model to use path (string): Path to video file ''' model_dict = {'alexnet': vision.alexnet, \ 'vgg16': vision.vgg16_bn, \ 'vgg19': vision.vgg19_bn, \ 'resnet18': vision.resnet18_v2, \ 'resnet50': vision.resnet50_v2, \ 'densenet121': vision.densenet121, \ 'squeezenet': vision.squeezenet1_1, \ 'mobilenetv1': vision.mobilenet1_0, \ 'mobilenetv2': vision.mobilenet_v2_1_0} # Model classes ctx = mx.cpu() load_net = model_dict[model] net = load_net(pretrained=True, ctx=ctx) v = cv2.VideoCapture(path) feats = [] k = 0 while (v.isOpened() and k < 250): ret, img = v.read() if ret: if model == 'squeezenet': # Input size to squeezenet is different img = transform_eval(nd.array(img), resize_short=350, crop_size=299) # Squeezenet outputs 512 feature maps. Applying global pooling feats.append( np.mean(np.mean(net.features(img).asnumpy(), axis=-1), axis=-1)) else: img = transform_eval(nd.array(img)) feats.append(net.features(img).asnumpy()) k += 1 else: break return feats
def mxnet_process_img(path): # Load Images img = image.imread(path) # Transform img = transform_eval(img) img_arr = img.asnumpy() if len(img_arr) == 3: img_arr = np.expand_dims(img_arr, axis=0) img_tf_tensor = tf.constant(img_arr) # np.random.seed(30) # img = nd.array(np.random.randn(1, 3, 600, 800)) # img_tf_tensor = tf.constant(img.asnumpy()) img_tf_tensor = tf.transpose(img_tf_tensor, [0, 2, 3, 1]) return img, img_tf_tensor
def get_embedding_advance(input_pic): # Load Images img = image.imread(input_pic) # Transform img = transform_eval(img).copyto(ctx[0]) pred = None use_layers = [len(seq_net) - 1] # [i for i in range(len(seq_net))] for i in range(len(seq_net)): img = seq_net[i](img) if i in use_layers: # print(img.shape) pred = img[0] break return pred.asnumpy().tolist()
def predict(pic): # If using different model, change below model_name = 'ResNet50_v2' net = get_model(model_name, pretrained=True) classes = net.classes # Take image and return ndarray img = image.imdecode(pic) # Default data preprocessing img = transform_eval(img) pred = net(img) topK = 1 ind = nd.topk(pred, k=topK)[0].astype('int') for i in range(topK): result = [ classes[ind[i].asscalar()], nd.softmax(pred)[i][ind[i]].asscalar() ] return result
def _mxnet2h5(model, name, blocks=(3, 4, 6, 3)): from mxnet import nd, image from gluoncv.model_zoo import get_model from gluoncv.data.transforms.presets.imagenet import transform_eval m_name = name m_name = m_name.replace("18", "") m_name = m_name.replace("35", "") m_name = m_name.replace("50", "") m_name = m_name.replace("101", "") m_name = m_name.replace("152", "") m_name = m_name.replace("_", "") img = image.imread("/Users/bailang/Documents/pandas.jpg") img = transform_eval(img) net = get_model(name, pretrained=True) pred = net(img) topK = 5 ind = nd.topk(pred, k=topK)[0].astype('int') print('The input picture is classified to be') for i in range(topK): print('\t[%s], with probability %.3f.'% (ind[i].asscalar(), nd.softmax(pred)[0][ind[i]].asscalar())) mx_weights = net.collect_params() # for k, v in mx_weights.items(): # print(k) name_map = _get_weight_name_map(blocks) # for k, v in name_map.items(): # print(k, v) for weight in model.weights: w_name = weight.name mx_name = m_name + "_" + name_map[w_name] mx_w = mx_weights[mx_name].data().asnumpy() if len(mx_w.shape) == 4: mx_w = mx_w.transpose((2, 3, 1, 0)) if len(mx_w.shape) == 2: mx_w = mx_w.transpose((1, 0)) weight.assign(mx_w)
def get_attribute(self, image): """Face attribute predictor. Parameters ---------- image: NDArray. The NDArray data format for MXNet to process, such as (H, W, C). Returns ------- type: tuple Results of Face Attribute Predict: (str(gender), int(age), str(expression)). """ img = transform_eval(image, resize_short=self._image_size, crop_size=self._image_size) img = img.as_in_context(self.ctx[0]) tic = time.time() pred = self.net(img) toc = time.time() - tic print('Attribute inference time: %fms' % (toc * 1000)) topK = 1 topK_age = 6 topK_exp = 2 age = 0 ind_1 = nd.topk(pred[0], k=topK)[0].astype('int') ind_2 = nd.topk(pred[1], k=topK_age)[0].astype('int') ind_3 = nd.topk(pred[2], k=topK_exp)[0].astype('int') for i in range(topK_age): age += int( nd.softmax(pred[1])[0][ind_2[i]].asscalar() * self.attribute_map2[1][ind_2[i].asscalar()]) gender = self.attribute_map2[0][ind_1[0].asscalar()] if nd.softmax(pred[2])[0][ind_3[0]].asscalar() < 0.45: expression = self.attribute_map2[2][7] else: expression_1 = self.attribute_map2[2][ind_3[0].asscalar()] expression_2 = self.attribute_map2[2][ind_3[1].asscalar()] return (gender, age, (expression_1, expression_2))
def classifyImage(model_name, input_pic): # Load Model # Assume pretrained net = get_model(model_name, pretrained=True) classes = net.classes # Load Images img = image.imread("images/" + input_pic) # Transform img = transform_eval(img) pred = net(img) # Display the top 5 classes from prediction topK = 5 ind = nd.topk(pred, k=topK)[0].astype('int') print('The input picture is classified to be') for i in range(topK): print('\t[%s], with probability %.3f.' % (classes[ind[i].asscalar()], nd.softmax(pred)[0][ind[i]].asscalar()))
def _mxnet2h5(model, blocks, name): from gluoncv.model_zoo import get_model from mxnet import nd, image from gluoncv.data.transforms.presets.imagenet import transform_eval net = get_model(name, pretrained=True) m_weights = net.collect_params() # for k, v in m_weights.items(): # print(k, v.shape) img = image.imread("/home/bail/Documents/pandas.jpg") img = transform_eval(img) net = get_model(name, pretrained=True) pred = net(img) topK = 5 ind = nd.topk(pred, k=topK)[0].astype('int') print('The input picture is classified to be') for i in range(topK): print('\t[%s], with probability %.3f.'% (ind[i].asscalar(), nd.softmax(pred)[0][ind[i]].asscalar())) name_map = _get_weight_name_map(blocks, False) # for k, v in name_map.items(): # print(k, v) for w in model.weights: mw = m_weights[name_map[w.name]].data().asnumpy() # print(w.name, w.shape.as_list()) if len(mw.shape) == 4: mw = mw.transpose((2, 3, 1, 0)) if len(mw.shape) == 2: mw = mw.transpose((1, 0)) w.assign(mw) del net
model='alexnet', input_pic='./ILSVRC2012_val_00000001.png', debug_nodes=[ "alexnet0_conv0_fwd_output", "alexnet0_conv1_fwd_output", "alexnet0_conv2_fwd_output", "alexnet0_conv3_fwd_output", "alexnet0_conv4_fwd_output", "alexnet0_dense0_fwd_output", "alexnet0_dense1_fwd_output", "alexnet0_dense2_fwd_output", "alexnet0_dropout0_fwd_output", "alexnet0_dropout1_fwd_output", "alexnet0_flatten0_flatten0_output", "alexnet0_pool0_fwd_output", "alexnet0_pool1_fwd_output", "alexnet0_pool2_fwd_output" ]) # 预处理模型以及图片 net = get_model(params.model, pretrained=True) img = image.imread(params.input_pic) img = transform_eval(img, resize_short=224, crop_size=224) wxf.export(img.asnumpy(), 'input.wxf', target_format='wxf') # 列出可选的输出节点 nodes = net(symbol.var('flow')).get_internals().list_outputs() wxf.export(nodes, 'nodes.wxf', target_format='wxf') def debug_net(net): data = symbol.var('flow') internals = net(data).get_internals() hooks = [internals[i] for i in params.debug_nodes] new = gluon.SymbolBlock(hooks, data, params=net.collect_params()) return new
11: ([1, 1, 2, 2, 2], [64, 128, 256, 512, 512]), 13: ([2, 2, 2, 2, 2], [64, 128, 256, 512, 512]), 16: ([2, 2, 3, 3, 3], [64, 128, 256, 512, 512]), 19: ([2, 2, 4, 4, 4], [64, 128, 256, 512, 512]) } from gluoncv.data.transforms.presets.imagenet import transform_eval from mxnet import nd, image from gluoncv.model_zoo.model_store import get_model_file from numpy.testing import assert_array_almost_equal if __name__ == "__main__": ctx = mx.gpu(0) input_pic = '../gluon-cv/street.jpg' img = image.imread(input_pic) img = transform_eval(img).as_in_context(ctx) print(img.shape) for num_layers in [16, 19]: for bn in [False, True]: _bn = '_bn' if bn else '' name = 'vgg{}{}'.format(num_layers, _bn) model = get_model_file(name, tag=True, root='models') print('--------------- {} -------------'.format(model)) layers, filters = vgg_spec[num_layers] vgg = VGG(layers, filters, batch_norm=bn) vgg.initialize(ctx=ctx) vgg.load_parameters(model, ctx, ignore_extra=True) vggbase = VGGBase(layers, filters, batch_norm=bn) vggbase.initialize(ctx=ctx) vggbase.import_params(model, ctx)
'https://gist.githubusercontent.com/zhreshold/', '4d0b62f3d01426887599d4f7ede23ee5/raw/', '596b27d23537e5a1b5751d2b0481ef172f58b539/', 'imagenet1000_clsid_to_human.txt' ]) synset_name = 'imagenet1000_clsid_to_human.txt' img_path = download_testdata(img_url, 'cat.png', module='data') synset_path = download_testdata(synset_url, synset_name, module='data') with open(synset_path) as f: synset = eval(f.read()) # Load Images img = image.imread(img_path) # Transform img = transform_eval(img) img = img.as_in_context(ctx) run_cnt = 1000 start_time = time.time() for i in range(run_cnt): pred = net(img) end_time = time.time() inference_time = (end_time - start_time) / float(run_cnt) * 1000 # ms print("Inference time: {:6.1f} ms".format(inference_time)) topK = 5 ind = nd.topk(pred, k=topK)[0].astype('int') print('The input picture is classified to be') for i in range(topK):
def pred_images(): # Start time start_time = time.time() # Get the image directories find_all_images() # Dictionary that stores all the information to be saved to a file save_data = {} # Loops through all images for images in pictures: # Updates to the right directory os.chdir(os.path.dirname(images)) # Create the dictionary to hold all the classes and confidences save_data[images] = {} # Load Images img = image.imread(os.path.basename(images)) # Transform img = transform_eval(img) # Prediction pred = net(img) # Prints the prediction ind = nd.topk(pred, k=opt.top_k)[0].astype('int') if opt.display_in_terminal: for i in range(opt.top_k): pred_obj = classes[ind[i].asscalar()] pred_score = nd.softmax(pred)[0][ind[i]].asscalar() if i == 0: print(f'\n{os.path.basename(images)} is classified to be:') print( f'\t{pred_obj}, with probability {math.floor(pred_score * 1000) / 1000}' ) # Now goes through through each class and gets the confidence ind = nd.topk(pred, k=1000)[0].astype('int') for simple in range(1000): pred_obj = classes[ind[simple].asscalar()].replace(' ', '_') pred_score = nd.softmax(pred)[0][ind[simple]].asscalar() save_data[images][pred_obj] = pred_score # Saves the results to a json file (if requested to do so) if opt.save_to_file: try: os.mkdir(os.path.dirname(directory)) except FileExistsError: # Nothing will happen if the file exists because it will simply put it in that directory pass # Sets the proper directory os.chdir(os.path.dirname(directory)) # Saves the data with open(os.path.basename(directory) + ".json", "w") as file_obj: json.dump(save_data, file_obj, indent=2, sort_keys=True) # Display time to run endtime = time.time() print(f'{endtime - start_time}')