コード例 #1
0
def visualizer_filter_input(model, images_per_row, img_width, img_height,
                            img_margin, layers_name):
    '''可视化过滤器最大化的网络输入
    @param model          网络模型
    @param images_per_row 网格图像每行显示图像单元个数
    @param img_width      输入图像宽度
    @param img_height     输入图像高度
    @param img_margin     网格图像单元间隙
    @param layers_name    卷积层向量表
    '''
    for layer_name in layers_name:
        #type(n_features)#=> <class 'tensorflow.python.framework.tensor_shape.Dimension'>
        n_features = model.get_layer(layer_name).output.shape[-1]  #卷积核个数
        rows = n_features // images_per_row  #图像单元行数
        rows = rows.value
        cols = images_per_row  #图像单元列数

        #初始化图像网格[rows{height} x cols{width}]
        display_grid = np.zeros(
            (rows * img_height + (rows - 1) * img_margin,
             cols * img_width + (cols - 1) * img_margin, 3))

        # We'll tile each filter into this big horizontal grid
        print('Generating layer of %s ......' % (layer_name))
        pbar = ShowProcess(100)
        for row in range(rows):
            for col in range(cols):
                filter_index = row * cols + col
                filter_img = generate_pattern(model,
                                              layer_name,
                                              filter_index,
                                              steps=gen_pat_steps,
                                              img_width=img_width,
                                              img_height=img_height)
                row_start = row * img_height + row * img_margin
                row_end = row_start + img_height
                col_start = col * img_width + col * img_margin
                col_end = col_start + img_width
                #print(display_grid.shape)
                #print('row_start:%d,row_end:%d,col_start:%d,col_end:%d'%(row_start,row_end,col_start,col_end))
                display_grid[row_start:row_end, col_start:col_end] = filter_img
                pbar.show_process((filter_index * 100 / (rows * cols)))
        pbar.show_process(100)
        # Display the results grid
        plt.figure(figsize=(20, 20))
        plt.imshow(display_grid)
        plt.show()
コード例 #2
0
# In[32]:

for layer_name in layers_name:
    #type(n_features)#=> <class 'tensorflow.python.framework.tensor_shape.Dimension'>
    n_features = model.get_layer(layer_name).output.shape[-1]  #卷积核个数
    rows = n_features // images_per_row  #图像单元行数
    rows = rows.value
    cols = images_per_row  #图像单元列数

    #初始化图像网格[rows{height} x cols{width}]
    display_grid = np.zeros((rows * img_height + (rows - 1) * img_margin,
                             cols * img_width + (cols - 1) * img_margin, 3))

    # We'll tile each filter into this big horizontal grid
    print('Generating layer of %s ......' % (layer_name))
    pbar = ShowProcess(100)
    for row in range(rows):
        for col in range(cols):
            filter_index = row * cols + col
            filter_img = generate_pattern(model,
                                          layer_name,
                                          filter_index,
                                          steps=gen_pat_steps,
                                          img_width=img_width,
                                          img_height=img_height)
            row_start = row * img_height + row * img_margin
            row_end = row_start + img_height
            col_start = col * img_width + col * img_margin
            col_end = col_start + img_width
            #print(display_grid.shape)
            #print('row_start:%d,row_end:%d,col_start:%d,col_end:%d'%(row_start,row_end,col_start,col_end))
コード例 #3
0
def detect_img(arg, yolo):
    '''批量图像目标检测,把结果保存在数据文件
    @param arg  参数
        arg.JPEGImages ----图片目录
        arg.imagesetfile --测试图片集,文本文件,每行标识一个图片名称(不含扩展名) ,如:
                2008_00001
                2008_00002
        arg.resultfile ----保存结果文件,CSV格式,第一行为字段名,其余每行为一条记录,如:
            ,filename,score,x,y,w,h,classid,classname
           0,2008_000001,0.987,10,20,30,50,0,dog
           1,2008_000001,0.78 ,30,50,200,100,1,cat
    @param yolo YOLO对象,对象构造,如:
        yolo=YOLO({'score':0.3,'iou':0.5}) #详细参数支持见YOLO实现代码 _default={...}
    '''
    images_path = arg.JPEGImages  #图片目录
    images_set_file = arg.imagesetfile  #测试图片集
    results_file = arg.resultfile  #结果输出文件
    print('images_path:', images_path)
    print('images_set_file:', images_set_file)
    print('results_file:', results_file)

    #进度条
    max_steps = 100
    process_bar = ShowProcess(max_steps, '', '', 'OK')

    #读取测试图片集
    with open(images_set_file, 'r') as f:
        lines = f.readlines()
    imagenames = [x.strip() for x in lines]
    num_images = len(imagenames)  #测试图片集数量

    #用pd.DataFrame保存结果
    df = pd.DataFrame(columns=[
        'filename', 'score', 'top', 'left', 'bottom', 'right', 'classid',
        'classname'
    ],
                      dtype=np.float32)
    key_index = 0  #关键字值(整型递增)

    #遍历检测图片
    for i, sname in enumerate(imagenames):
        sfile = os.path.join(images_path, '%s.jpg' % (sname))
        image = Image.open(sfile)
        #图片检测
        out_boxes, out_scores, out_classes, out_classesname = yolo.detect_image(
            image, draw=False)
        #检测结果解析,并向DataFrame添加一行
        for n in range(len(out_boxes)):
            newline = pd.DataFrame(
                {
                    'filename': sname,
                    'score': out_scores[n],
                    'top': out_boxes[n, 0],
                    'left': out_boxes[n, 1],
                    'bottom': out_boxes[n, 2],
                    'right': out_boxes[n, 3],
                    'classid': out_classes[n],
                    'classname': out_classesname[n]
                },
                index=[key_index])
            df = df.append(newline)
            key_index += 1
        #进度条显示
        process_bar.show_process(int(100. * i / num_images), '',
                                 '%d/%d' % (i, num_images))
    process_bar.show_process(100, '', '%d/%d' % (num_images, num_images))
    print(df)
    #保存结果到CSV文件
    df.to_csv(results_file)
    #关闭会话
    yolo.close_session()
コード例 #4
0
def plot_layer_filter(model,
                      layers_name,
                      target_size=(150, 150),
                      img_margin=1,
                      images_per_row=16,
                      gen_pat_steps=20):
    '''可视化卷积神经网络的过滤器
    @param model                训练好的网络模型
    @param layers_name    [list]输出层名称,一般为卷积层
    @param img_width      [int ]过滤器图像宽度
    @param img_height     [int ]过滤器图像高度
    @param img_margin     [int ]图像单元间隙
    @param images_per_row [int ]每行显示图像单元个数
    @param gen_pat_steps  [int ]生成过滤器图像迭代次数
    
    使用范例:
        model=...                                #创建/加载模型/模型编译/模型训练 
        layers_name=['conv_1','conv_2','conv_3'] #设置输出层
        plot_layer_filter(model,layers_name)
        
    '''

    #把数据图像化输出
    def deprocess_image(x):
        '''数据图像化输出
        @param x  数据
        @return   图像化输出结果
        处理流程:
            1. x归一化处理:mean=0,std=0.1
            2. x整体抬升0.5,并作[0,1]裁剪
            3. x整体乘于255,并作[0,255]裁剪
        '''
        # normalize tensor: center on 0., ensure std is 0.1
        x -= x.mean()
        x /= (x.std() + 1e-5)
        x *= 0.1

        # clip to [0, 1]
        x += 0.5
        x = np.clip(x, 0, 1)

        # convert to RGB array
        x *= 255
        x = np.clip(x, 0, 255).astype('uint8')
        return x

    #生成过滤器的最大化输入
    def generate_pattern(model,
                         layer_name,
                         filter_index,
                         steps=40,
                         img_width=150,
                         img_height=150):
        '''生成过滤器的最大化输入
        @param model        网络模型
        @param layer_name   网络层名称,通过名称获取网络层
        @param filter_index 卷积核索引
        @param steps        构造迭代次数
        @param img_width    网络输入图像宽度
        @param img_height   网络输入图像高度
        @return 网络输入图像数据
        '''
        # Build a loss function that maximizes the activation
        # of the nth filter of the layer considered.
        layer_output = model.get_layer(layer_name).output  #网络层输出
        loss = K.mean(layer_output[:, :, :, filter_index])  #构造损失函数

        # Compute the gradient of the input picture wrt this loss
        grads = K.gradients(loss, model.input)[0]  #梯度计算

        # Normalization trick: we normalize the gradient
        grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)  #梯度归一化处理

        # This function returns the loss and grads given the input picture
        iterate = K.function([model.input], [loss, grads])  #迭代函数

        # We start from a gray image with some noise
        input_img_data = np.random.random(
            (1, img_height, img_width, 3)) * 20 + 128.  #初始化网络输入图像

        # Run gradient ascent for 40 steps
        step = 1.
        for i in range(steps):
            loss_value, grads_value = iterate([input_img_data])
            input_img_data += grads_value * step

        img = input_img_data[0]
        return deprocess_image(img)

    img_height, img_width = target_size
    #=============================
    for layer_name in layers_name:
        #type(n_features)#=> <class 'tensorflow.python.framework.tensor_shape.Dimension'>
        n_features = model.get_layer(layer_name).output.shape[-1]  #卷积核个数
        rows = n_features // images_per_row  #图像单元行数
        rows = rows.value
        cols = images_per_row  #图像单元列数

        #初始化图像网格[rows{height} x cols{width}]
        display_grid = np.zeros(
            (rows * img_height + (rows - 1) * img_margin,
             cols * img_width + (cols - 1) * img_margin, 3))

        # We'll tile each filter into this big horizontal grid
        print('Generating layer of %s ......' % (layer_name))
        pbar = ShowProcess(100)
        for row in range(rows):
            for col in range(cols):
                filter_index = row * cols + col
                filter_img = generate_pattern(model,
                                              layer_name,
                                              filter_index,
                                              steps=gen_pat_steps,
                                              img_width=img_width,
                                              img_height=img_height)
                row_start = row * img_height + row * img_margin
                row_end = row_start + img_height
                col_start = col * img_width + col * img_margin
                col_end = col_start + img_width
                #print(display_grid.shape)
                #print('row_start:%d,row_end:%d,col_start:%d,col_end:%d'%(row_start,row_end,col_start,col_end))
                display_grid[row_start:row_end, col_start:col_end] = filter_img
                pbar.show_process((filter_index * 100 / (rows * cols)))
        pbar.show_process(100)
        # Display the results grid
        plt.figure(figsize=(20, 20))
        plt.imshow(display_grid)
        plt.title('plot_layer_filter(layer_name:%s)' % (layer_name))
        plt.show()
コード例 #5
0
funs.GatherFilesEx(src_path, files)
files_num = len(files)
print('files_num:%s' % (files_num))

# 文件分类迁移

# In[4]:

#自动创建分类目录
for k, v in dicts.items():
    if not os.path.exists(v):
        os.makedirs(v)

#文件迁移
max_steps = 100
pb = ShowProcess(max_steps, 'head', 'tail', 'OK')
for i, sfile in enumerate(files):
    file_name = os.path.basename(sfile)
    for k, v in dicts.items():
        #正则表达式判断
        if re.search(r'_%s[0-9]+_' % (k), file_name) and os.path.exists(sfile):
            shutil.move(sfile, '{}/{}'.format(v, file_name))  #移动文件或重命名
            if i % 1000 == 0:
                pb.show_process(int(i * 100 / files_num))
            break
pb.show_process(100)

# In[ ]:

# In[ ]: