목록[Python]/코드 (12)
코드민수
경로 A에서 경로 B로 n 개의 파일을 복사하는 파이썬 코드입니다 import os import random import shutil from tqdm import tqdm def copy_random_files(source_path, destination_path, num_files): files = os.listdir(source_path) # 파일 목록에서 랜덤하게 num_files 개의 파일 선택 random_files = random.sample(files, num_files) # 파일을 경로 B로 복사 for file in tqdm(random_files): source_file = os.path.join(source_path, file) destination_file = os.path.joi..

해당 폴더에 있는 V2 포맷의 events.out.tfevents 파일을 tensorboard를 사용해서 열어보겠습니다. 1. tensorflow와 tensorboard가 설치되어 있는 가상환경 실행 2. 명령문 실행 tensorboard --logdir= V2 등 파일이 아닌 해당 파일이 있는 경로를 입력하셔야 합니다. 3. http://localhost:6006 접속

from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) map = Basemap(projection='merc', lat_0=37.35, lon_0=126.58, resolution='h', urcrnrlat=44, llcrnrlat=32, llcrnrlon=121.5, urcrnrlon=132.5) map.drawcoastlines() map.drawcountries() map.drawmapboundary() plt.savefig('map.png', bbox_inches='tight') plt.savefig() 에 bbox_inches='tight'를 추가하면 됩니다.
from glob import glob import numpy as np from osgeo import gdal glob_image_dir = 'D:/temp/image/*.tif' # 이미지 확장자 수정 image_filename_list = glob(glob_image_dir) def load_images(): global image_filename_list image_list = [] for idx, image_filename in enumerate(image_filename_list): image_ds = gdal.Open(image_filename) image = image_ds.ReadAsArray().astype(np.int16) # 0~255 이미지는 int8로 충분 image_list...
tensorflow에서 GPU 사용 가능 여부 확인 from tensorflow.python.client import device_lib device_lib.list_local_devices() Pytorch에서 GPU 사용 가능 여부 확인 import torch print(torch.cuda.is_available()) CUDA, cudnn, tensorflow, pytorch가 버전에 맞게 잘 설치되어 있어야 작동합니다.
오픈소스 딥러닝 데이터세트를 사용할 때 간혹 이미지는 있지만 레이블이 없는 경우가 있습니다. 해당 이미지들을 사용해서 새로 레이블을 구축하기 위해 레이블이 없는 이미지만 새로운 폴더에 복사하는 간단한 파이썬 코드를 구현했습니다. import os from tqdm import tqdm import shutil image_dir = '경로1' # 이미지 폴더 label_dir = '경로2' # 레이블 폴더 save_dir = '경로3' # 레이블이 없는 이미지를 복사할 경로 f_imgs = os.listdir(image_dir) for i in tqdm(f_imgs): image = os.path.join(image_dir, i) label = os.path.join(label_dir, i.replace(..

객체 탐지(Object Detection) 모델을 학습하기 위해 사용하는 JSON 포맷 라벨 데이터에서 각 클래스별 객체 수를 count해서 히스토그램으로 시각화 하는 코드입니다. import os import json import matplotlib.pyplot as plt path = '경로' # JSON 파일 있는 경로 classes = {'Glass': 1, 'Metal': 2, 'Wood': 3, 'Plastic': 4, 'Fish_trap': 5, 'Rubber_tire': 6, 'Rubber_etc': 7, 'Etc': 8} num = len(classes) histogram = [0] * num for file in os.listdir(path): with open(os.path.join(..
지정된 디렉토리에서 중복된 이미지를 찾아서 삭제하는 방법입니다. 이미지 파일이 많고 파일명에 규칙이 없는 경우 중복 파일을 찾는 것은 번거롭고 어려운 작업입니다. 해당 코드에서는 MD5 알고리즘을 사용하여 이미지를 고유하게 식별할 수 있는 Hash 값을 추출하고 이를 기준으로 중복 여부를 판단하여 중복 이미지를 삭제했습니다. from PIL import Image import os import hashlib def find_duplicate_images(rootdir): hash_dict = {} duplicates = [] for subdir, dirs, files in os.walk(rootdir): for file in files: filepath = os.path.join(subdir, file)..