学習日記2

8987 ワード

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の反転を使用して画像をデータ拡張しますが、yolo 3の効果はあまりよくありません.コードは以下の通りです.
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ベースyolov 3モデルの使用、keras-yolo 3.labelImgを使用してデータを表示する、trainを使用する.pyデータトレーニングを行い、cfgファイルを構成し、yolov 3モデルのパラメータを調整した後、python convertを使用します.py -w yolov3.cfg yolov3.weights model_data/yolo_weights.h 5は重みとcfgを変換する.h 5ファイルはモデルとして重みをロードする.
現在、いくつかの重要なパラメータはinput_であると考えられています.shape,anchors,filters,filters数=(class+5)*(anchors個数//スケール)、yolov 3の3つのスケール、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])