여러 HTML 파일에 로고를 박아줘야하는 작업이 생김.

- 반복 처리를 위해 코드로 작업화.

 

생성된 tag 구조

--body

  --div

    --svg

      --path

      --path

from bs4 import BeautifulSoup

page = open('./test.html', 'rt', encoding='utf-8').read()
soup = BeautifulSoup(page, 'html.parser')

# style 태그의 내용변경.
soup.style.string = 'font-family: ff-clan-web-pro, "Helvetica Neue", Helvetica, sans-serif;    font-weight: 400;    font-size: 0.875em;    line-height: 1.71429;    *,    *:before,    *:after {      -webkit-box-sizing: border-box;      -moz-box-sizing: border-box;      box-sizing: border-box;    }    body {      margin: 0; padding: 0;    }    .logo {            position: absolute;            right: 160px;            bottom: 35px;            z-index: 1;            width: 5%;          }'

# 새로운 태그 생성
new_tag_1 = soup.new_tag("div", **{'class':'logo'})
new_tag_2 = soup.new_tag("svg", **{'xmlns': 'http://www.w3.org/2000/svg','width': '250','height': '75','viewBox': '0 0 1141 297','version': '1.1',})
new_tag_3 = soup.new_tag("path", **{'stroke':'none', 'fill':'#04439c', 'fill-rule':'evenodd'})
new_tag_4 = soup.new_tag("path", **{'stroke':'none', 'fill':'#e50717', 'fill-rule':'evenodd'})
soup.body.div.append(new_tag_1)
soup.body.div.div.append(new_tag_2)
soup.body.div.div.svg.append(new_tag_3)
soup.body.div.div.svg.append(new_tag_4)

html = soup.prettify('utf-8')
with open("./output.html", "wb") as file:
    file.write(html)

 

우분트 서버 숨겨진 와이파이 연결 

 

1) sudo vi /etc/netplan/50-cloud-init.yaml

     add line : hidden: true

2) sudo netplan generate

3) sudo netplan apply

20.04 version
network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    wifis:
        wlan0:
            optional: true
            access-points:
                "Wifi_name":
                    password: "Wifi_password"
                    hidden: true
            dhcp4: true
            
            
18.04 version
network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    wifis:
        wlan0:
            hidden: true
            optional: true
            access-points:
                "Wifi_name":
                    password: "Wifi_password"
            dhcp4: true

 

위경도 좌표 데이터를 가지고 데이터 프레임으로 csv 파일을 만들 일이 생김.

 

데이터 형태  

coord_list = [[위도, 경도], [위도, 경도], [위도, 경도] ... [위도, 경도]]

 

이러한 데이터는 데이터 프레임으로 제작 시 오류 발생.

-> dict 형식으로 데이터 프레임에 넣어준다.

coord_df = pd.DataFrame({‘위경도‘: coord_list})
route_df.to_csv(r'./route.csv', encoding = "euc-kr")

csv 파일로 저장후 다시 불러오게 되면, float로 들어갔던 위경도 데이터가 str로 바껴서 나오는 마법이 걸린다.

converters를 사용해 주자.

route_df = pd.read_csv(r’./route.csv’, encoding = “utf-8”, converters={‘위경도’: pd.eval})

 

'Study > Code' 카테고리의 다른 글

Python BeautifulSoup Html Tag 생성하기.  (0) 2023.05.09
PIL Image Crop & Paste  (0) 2020.09.18
python href 크롤링  (0) 2019.08.13
python list 중복 값 카운터하기  (0) 2019.02.22
numpy를 이용해 새로운 array 만들기.  (0) 2019.02.16

학습 시킨 weight 파일을 가지고 있어야함.

from model import resnet
import torch.onnx
import os
import torch.backends.cudnn as cudnn


os.environ['CUDA_VISIBLE_DEVICES'] = '5'
cudnn.benchmark = True

model = resnet.ResNet18().cuda()

map_location = lambda storage, loc: storage
if torch.cuda.is_available():
    map_location = None
    model.load_state_dict(torch.load('./150_epoch/model_150.pth', map_location=map_location)) 

model.eval()

batch_size = 256
x = torch.randn(batch_size, 3, 32, 32, requires_grad=True).cuda()

torch.onnx.export(model,               # 실행될 모델
                  x,                         # 모델 입력값 (튜플 또는 여러 입력값들도 가능)
                  "./super_resolution.onnx",   # 모델 저장 경로 (파일 또는 파일과 유사한 객체 모두 가능)
                  export_params=True,        # 모델 파일 안에 학습된 모델 가중치를 저장할지의 여부
                  opset_version=10,          # 모델을 변환할 때 사용할 ONNX 버전
                  do_constant_folding=True,  # 최적하시 상수폴딩을 사용할지의 여부
                  input_names = ['input'],   # 모델의 입력값을 가리키는 이름
                  output_names = ['output'], # 모델의 출력값을 가리키는 이름
                  dynamic_axes={'input' : {0 : 'batch_size'},    # 가변적인 길이를 가진 차원
                                'output' : {0 : 'batch_size'}})

 

학습을 진행하며 특정 조건들을 수집 후 해당 조건을 가지고 기존 데이터 셋에서 데이터를 삭제할 일이 생김.

데이터셋을 불러오는 코드부터 전체를 짜긴 귀찮으니.. torchvision에서 제공하고 있는 torchvision.datasets.imagefolder를 이용하자!

 

먼저 해당 페이지에서 코드를 가져오자  github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py

 

ImageFolder를 사용하는 방법은 간단하다.

- 데이터가 있는 경로만 넣어주면 바로 dataset 형태로 만들어지며, DataLoader에 넘겨주기만 하면 된다.

- 보통의 경우 x(input), y(target) 데이터를 받아서 학습을 시켜주게 된다. 

- 여기서 우리는 x,y 값 말고도 다른 정보를 얻고 싶고, 정보를 얻어오는 방법과 데이터를 핸들링하는 방법을 알아본다.

- 데이터셋의 경로 구조

----/test

           ---class1

           ---class2

import imagefolder

test_dataset = imagefolder.ImageFolder('./test', transform=test_transform)


test_loader = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=1,
                                          shuffle=False,
                                          num_workers=4)
                                          
for i, (input, target) in enumerate(train_loader):
	print(input, tartget)

수정 한 부부은 # edit 으로 표시했고 추가한 기능은 다음과 같다.

- loader에서 데이터의 index와 파일명을 추가로 받아옴

- 특정 조건을 만족할 경우 데이터 셋 삭제

 

수정한 코드내용

- self.samples, self.targets : 기존에 tuple 형식이던 데이터를 각각 list 형태로 받아옴

- self.file_names : file_name을 가지고 데이터를 삭제할 것이기 때문에 추가해주고 numpy array 형식으로 변환

- remove_data : 해당 함수를 말들어 filename이 같을 경우에 데이터를 삭제해줌 -> x, y 둘다 삭제

- __getitem__ : path, target을 각각 받도록 수정, return 값들 추가 (index, filename)

       - 해당 함수의 경우 index를 인자로 받아서 해당 index에 해당하는 파일들만 return 해줌

from torchvision.datasets.vision import VisionDataset
from PIL import Image

import numpy as np
import os
import os.path

class DatasetFolder(VisionDataset):
    def __init__(self, root, loader, extensions=None, transform=None,
                 target_transform=None, is_valid_file=None):
        super(DatasetFolder, self).__init__(root, transform=transform,
                                            target_transform=target_transform)
        classes, class_to_idx = self._find_classes(self.root)
        samples, file_names = make_dataset(self.root, class_to_idx, extensions, is_valid_file)
        if len(samples) == 0:
            msg = "Found 0 files in subfolders of: {}\n".format(self.root)
            if extensions is not None:
                msg += "Supported extensions are: {}".format(",".join(extensions))
            raise RuntimeError(msg)
        self.loader = loader
        self.extensions = extensions
        self.classes = classes
        self.class_to_idx = class_to_idx

        # edit
        self.file_names = np.array(file_names)
        self.samples = [s[0] for s in samples]
        self.targets = [s[1] for s in samples]

	# edit
    def remove_data(self, filename):
        idx = np.where(self.file_names == filename)[0]
        self.targets = np.delete(self.targets, idx)
        self.samples = np.delete(self.samples, idx)
	
    def __getitem__(self, index):
      # edit
      path = self.samples[index]
      target = self.targets[index]

      sample = self.loader(path)

      if self.transform is not None:
      sample = self.transform(sample)
      if self.target_transform is not None:
      target = self.target_transform(target)

      # edit
      return sample, target, index, self.file_names[index]

    def _find_classes(self, dir):
        """
        Finds the class folders in a dataset.

        Args:
            dir (string): Root directory path.

        Returns:
            tuple: (classes, class_to_idx) where classes are relative to (dir), and class_to_idx is a dictionary.

        Ensures:
            No class is a subdirectory of another.
        """
        classes = [d.name for d in os.scandir(dir) if d.is_dir()]
        classes.sort()
        class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
        return classes, class_to_idx

    def __len__(self):
        return len(self.samples)

datasets에 접근

- DatasetFolder의 init 초기화 함수에서 설정했던 self 변수들에 접근이 가능하고

- remove를 했을 경우 데이터셋이 삭제된다.

import imagefolder

test_dataset = imagefolder.ImageFolder('./test', transform=test_transform)

test_loader = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=1,
                                          shuffle=False,
                                          num_workers=4)

print(len(test_dataset.samples), len(test_dataset.targets))
test_dataset.remove_data('20200401_161246.jpg')
print(len(test_dataset.samples), len(test_dataset.targets))
                                          
for i, (input, target, idx, file_name) in enumerate(train_loader):
	print(input, tartget, idx, file_name)

 

+ Recent posts