学习日记2

1利用xlsxwriter自动生成excel报表,在unbuntu下公式不会自动运算,在windows下可以,主要功能代码:

 workbook = xlsxwriter.Workbook('new_table.xlsx') 
    sheetslit = workbook.add_worksheet('slit') 
    sheetnormal = workbook.add_worksheet('normal')
    bold = workbook.add_format({'bold': 1})
    headings = ['图片名称','漏检个数', '总数', '遮挡1','遮挡2']
    sheetslit.write_row('A1', headings, bold)#
    sheetnormal.write_row('A1', headings, bold)#
    slit_count = 1
    slit_miss_count = 0
    slit_total_count = 0
    normal_count = 1
    normal_miss_count = 0
    normal_total_count = 0
    slit_set =set()
    normal_set = set()
    for p in path:
        #根据数据自动生成excel
        try:
            image = Image.open('/home/xd/Desktop/test_images/'+p)
        except:
            print('Open Error! Try again!')
            continue
        else:
            r_image,boxes = yolo.detect_image(image)
            for d in x:
                if d['name'] == p:
                    miss = d['num'] - boxes
                    if d['table_incline']==True:
                        worksheet = sheetslit
                        slit_count += 1
                        slit_miss_count += abs(miss)
                        slit_total_count += d['num']
                        worksheet.write('A'+str(slit_count), d['name'])
                        worksheet.write('B'+str(slit_count), miss)
                        worksheet.write('C'+str(slit_count), d['num'])
                        worksheet.write('D'+str(slit_count), d['seat_mask'])
                        worksheet.write('E'+str(slit_count), d['stand_mask'])
                        slit_set.add(miss)
#                        
                    else:
                        worksheet = sheetnormal
                        normal_count += 1
                        normal_miss_count += abs(miss)
                        normal_total_count += d['num']
                        worksheet.write('A'+str(normal_count), d['name'])
                        worksheet.write('B'+str(normal_count), miss)
                        worksheet.write('C'+str(normal_count), d['num'])
                        worksheet.write('D'+str(normal_count), d['seat_mask'])
                        worksheet.write('E'+str(normal_count), d['stand_mask'])
                        normal_set.add(miss)
                      
                    
                    break
    sheetnormal.write('A'+str(normal_count+1),'总计')
    sheetnormal.write('B'+str(normal_count+1),str(normal_miss_count))
    sheetnormal.write('C'+str(normal_count+1),str(normal_total_count))
    count = 1
    for ss in normal_set:
        sheetnormal.write('G'+str(count),ss)
        sheetnormal.write_formula('F'+str(count), '=COUNTIF(B2:B'+str(normal_count)+','+str(ss)+')')
        count += 1
        
    chart_normal = workbook.add_chart({'type':'pie'})
    global_miss_count = normal_miss_count + slit_miss_count
    chart_normal.set_title ({'name': '漏检个数'})
    chart_normal.set_style(3)    
    chart_normal.add_series({        
        'name':         '漏检个数',
        'categories': '=normal!$G$1'+':$G$'+str(count-1),
        'values':     '=normal!$F$2:$F$5'+str(normal_count)
       
    })
    print(sheetnormal.insert_chart('E52', chart_normal, {'x_offset': 25, 'y_offset': 10}))
    
    sheetslit.write('A'+str(slit_count+1),'总计')
    sheetslit.write('B'+str(slit_count+1),str(slit_miss_count))
    sheetslit.write('C'+str(slit_count+1),str(slit_total_count))
    print((slit_miss_count+normal_miss_count))
    count = 1
    for ss in slit_set:
        sheetslit.write_formula('F'+str(count), '=COUNTIF(B2:B'+str(slit_count)+','+str(ss)+')')
        sheetslit.write('G'+str(count),ss)
        count += 1
        
    chart_slit = workbook.add_chart({'type':'pie'})
    chart_slit.set_title ({'name': '漏检个数'})
    chart_slit.set_style(3)    
    chart_normal.add_series({        
        'name':         '漏检个数',
        'categories': '=normal!$G$1'+':$G$'+str(count-1),
        'values':     '=normal!$F$2:$F$5'+str(slit_count)
    })
    print(sheetslit.insert_chart('E52', chart_slit, {'x_offset': 25, 'y_offset': 10}))
    
    workbook.close()

2使用opencv的翻转对图片进行数据增强,但对于yolo3的效果不怎么好,代码如下

def deal_flip_image(image,xmin,ymin,xmax,ymax,flip_type):
    #水平翻转
    height = image.shape[0]
    width = image.shape[1]
    if flip_type==1:
        xmin = width - xmin
        xmax = width - xmax
    #垂直翻转
    elif flip_type==0:
        ymin = height - ymin
        ymax = height - ymax
    #垂直水平翻转
    else:
       xmin = width - xmin
       xmax = width - xmax
       ymin = height - ymin
       ymax = height - ymax
    return xmin,ymin,xmax,ymax
#    cv2.rectangle(image, (xmin, ymax), (xmax, ymin), (255, 0, 0), 2)
 # 获得根节点
def filp_augment(new_path= "D://train/transform/",new_image_path= "D://train/newimages/",old_path="D://train/Annotations/",old_image_path="D://train/Images/"):
    for p in os.listdir(old_path):
        for filp_type in [1,0,-1]:
            tree = ET.parse(old_path+p)
            root = tree.getroot()
            objects = root.findall("object")
            file_name = root.find("filename").text
            path = old_image_path + file_name
            orignal_image = cv2.imread(path)
            transfrom_image = cv2.flip(orignal_image, filp_type)  
            for item in objects:
                bndbox = item.find('bndbox')
                xmin = int(bndbox.find("xmin").text)
                ymin = int(bndbox.find("ymin").text)
                xmax = int(bndbox.find("xmax").text)
                ymax = int(bndbox.find("ymax").text)
    #        cv2.rectangle(orignal_image, (xmin, ymax), (xmax, ymin), (255, 0, 0), 2)
                xmin,ymin,xmax,ymax = deal_flip_image(transfrom_image,xmin,ymin,xmax,ymax,filp_type)
                bndbox.find("xmin").text = str(xmin)
                bndbox.find("ymin").text = str(ymin)
                bndbox.find("xmax").text = str(xmax)
                bndbox.find("ymax").text = str(ymax)
            prefix = {1:"horizontal_",0:"vertical_",-1:"h_v_"}[filp_type]
            new_file_name = prefix + file_name
            root.find("filename").text = new_file_name
            root.find("path").text = root.find("path").text.replace(file_name,new_file_name)
            cv2.imwrite(new_image_path + new_file_name,transfrom_image)
            tree.write(new_path+prefix + p,xml_declaration=True, encoding="utf-8") 
#        print((xmin,ymin,xmax,ymax))
      
def verify_augment(annotations_path,image_path):
    image = cv2.imread(image_path)
    tree = ET.parse(annotations_path)
    root = tree.getroot()
    objects = root.findall("object")
    for item in objects:
        bndbox = item.find('bndbox')
        xmin = int(bndbox.find("xmin").text)
        ymin = int(bndbox.find("ymin").text)
        xmax = int(bndbox.find("xmax").text)
        ymax = int(bndbox.find("ymax").text)
        cv2.rectangle(image, (xmin, ymax), (xmax, ymin), (255, 0, 0), 2)
    cv2.imshow("test", image)

3对github的基于keras的yolov3模型的使用,keras-yolo3。使用labelImg标注数据,使用train.py进行数据训练,配置cfg文件,调整yolov3模型的参数,之后使用python convert.py -w yolov3.cfg yolov3.weights model_data/yolo_weights.h5将权重和cfg转换成.h5文件作为模型载入权重。

目前认为几个比较重要的参数是input_shape,anchors,filters,filters数目=(class+5)*(anchors个数//尺度),yolov3三个尺度,anchors数目为9。anchors可以通过kmeans来获得。

4 对tensorboard有一定认识,使用 tensorboard --logdir=路径 命令,就可以在127.0.0.1:6006这个地址上查看计算图,网络结构,损失函数。对于keras,只需要在训练的callback里添加tensorboard对象就行了。

 logging = TensorBoard(log_dir=log_dir)

  #训练模型
        model.fit_generator(data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes),
                steps_per_epoch=max(1, num_train//batch_size),
                validation_data=data_generator_wrapper(lines[num_train:], batch_size, input_shape, anchors, num_classes),
                validation_steps=max(1, num_val//batch_size),
                epochs=100,
                initial_epoch=0,
                callbacks=[logging, checkpoint])

你可能感兴趣的:(work)