개발로 하는 개발

SketchHealer code 돌리기 본문

Projects/Capstone

SketchHealer code 돌리기

jiwon152 2023. 11. 22. 13:15

1. github clone

https://github.com/sgybupt/SketchHealer

 

2. download .npz file.

Numpy .npz files

 

Google 클라우드 플랫폼

로그인 Google 클라우드 플랫폼으로 이동

accounts.google.com

 

pip install gsutil
gsutil -m cp
"gs://quickdraw_dataset/sketchrnn/*.full.npz"

 

3. dependencies

-> virtual venv 먼저 만들기

python -m venv sketchHealer
source sketchHealer/bin/activate

//later -> deactivate

 

To run this code, you need to install
pytorch
torchvision
opencv
pillow
and some common dependency libraries.

 

pip install torch torchvision
pip install opencv-python
pip install pillow
pip install matplotlib

 

hyper parameter 변경

#hyper_params.py
def __init__(self):
    self.data_location = ''#location of origin data
    #/home/ubuntu/SketchHealer/dataset

 

pretrained model - encoderRNN_epoch_146000.pth - 경로 확인

 

 Verify that the file 'encoderRNN_epoch_146000.pth' exists at the specified location (./model_save/). Double-check the path and file name for any typos or discrepancies. - relative path

 

#inference.py line 367
'''
RuntimeError: Attempting to deserialize object on a CUDA device 
but torch.cuda.is_available() is False. 
If you are running on a CPU-only machine, 
please use torch.load with map_location=torch.device('cpu') 
to map your storages to the CPU.
M1, M2는 CUDA 지원 안되서 CPU로 돌려야함
 - CUDA, which is NVIDIA-specific, was not compatible with M1 Macs.
'''
    def load(self, encoder_name, decoder_name):
        if torch.cuda.is_available():
            saved_encoder = torch.load(encoder_name)
            saved_decoder = torch.load(decoder_name)
        else:
            saved_encoder = torch.load(encoder_name, map_location=torch.device('cpu'))
            saved_decoder = torch.load(decoder_name, map_location=torch.device('cpu'))
        self.encoder.load_state_dict(saved_encoder)
        self.decoder.load_state_dict(saved_decoder)

 

#sketch_processing.py line 453
'''
AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. 
To avoid this error in existing code, use `float` by itself. 
Doing this will not modify any behavior and is safe. 
If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; 
for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
    
'''
def scale_sketch(sketch, size=(448, 448)):
    [_, _, h, w] = canvas_size_google(sketch)
    if h >= w:
        sketch_normalize = sketch / np.array([[h, h, 1]], dtype=np.float32)
    else:
        sketch_normalize = sketch / np.array([[w, w, 1]], dtype=np.float32)
    sketch_rescale = sketch_normalize * np.array([[size[0], size[1], 1]], dtype=np.float32)
    return sketch_rescale.astype("int16")
#inference.py -> line 234 sketches_categroy_count 
'''
line 232, in conditional_generation
    category_name = sketch_dataset.category[category_flag].split(".")[0]
IndexError: list index out of range
-> trying to split without anything to split
'''

#category_name = sketch_dataset.category[category_flag].split(".")[0]

if category_flag < 17:
    category_name = sketch_dataset.category[category_flag].split(".")[0]
    count = 0
    category_count = sketch_dataset.sketches_categroy_count[category_flag]
    print(f"{category_name} start")

 

#decoder.py
'''
Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
  pi = F.softmax(pi.transpose(0, 1).squeeze()).view(len_out, -1, hp.M)  # torch.Size([130, 100, 20])
  
Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
  q = F.softmax(params_pen).view(len_out, -1, 3)
 '''
 
pi = F.softmax(pi.transpose(0, 1).squeeze(), dim = 0).view(len_out, -1, hp.M)  # torch.Size([130, 100, 20])

q = F.softmax(params_pen, dim = 0).view(len_out, -1, 3)

 

# 
'''
/Users/username/Documents/works/SketchHealer/utils/sketch_processing.py:451: RuntimeWarning: divide by zero encountered in divide
  sketch_normalize = sketch / np.array([[h, h, 1]], dtype=np.float32)
/Users/username/Documents/works/SketchHealer/utils/sketch_processing.py:455: RuntimeWarning: invalid value encountered in cast
  return sketch_rescale.astype("int16")
'''
if h != 0:
    sketch_normalize = sketch / np.array([[h, h, 1]], dtype=np.float32)
else:
    # Handle the case where h is zero



sketch_rescale = np.nan_to_num(sketch_rescale, nan=0).astype("int16")
python -u inference.py

 

 

npz file 분석

import numpy as np
# since the npz files are a object array, you have to set allow_pickle as True
# because the files are not in ascii encoding, you have to set the encoding to latin1
np.load("airplane.npz", allow_pickle=True, encoding='latin1')
data = np.load("airplane.npz", allow_pickle=True, encoding='latin1')

test_data = data['test']
train_data = data['train']
valid_data = data['valid']

print('Test Data Shape:', test_data.shape)
print('Train Data Shape:', train_data.shape)
print('Valid Data Shape:', valid_data.shape)

'''
NpzFile 'airplane.npz' with keys: test, train, valid
Test Data Shape: (2500,)
Train Data Shape: (70000,)
Valid Data Shape: (2500,)

'''

first_array_train = train_data[0]
print(first_array_train)

'''
[[-43 -13   0]
 [-52  -4   0]
 [-24  -6   0]
 [-16   1   0]
 [ -7  23   0]
 [  0  25   0]
 [  5  11   0]
 [ 13   5   0]
 [ 29   7   0]
 [ 88  13   0]
 [  3   5   0]
 [  0  18   0]
 [ -5  38   0]
 [ -3  80   0]
 [ 25  39   0]
 [  5   2   0]
 [ 10 -26   0]
 [ 13 -61   0]
 [  5 -35   0]
 [  0 -42   0]
 [  5  -3   0]
 [ 16   0   0]
 [ 52   8   0]
 [ 99   0   0]
 [  0 -12   0]
 [ -6 -13   0]
 [-23 -29   0]
 [-38 -27   0]
 [-49 -14   0]
 [ -4  -5   0]
 [  0 -42   0]
 [ 14 -93   0]
 [ -3 -13   1]]
 '''
 
 first_array_train = train_data[0, :]
'''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
'''

 

This data is also used for training the Sketch-RNN model. An open source, TensorFlow implementation of this model is available in the Magenta Project, (link to GitHub repo). You can also read more about this model in this Google Research blog post. The data is stored in compressed .npz files, in a format suitable for inputs into a recurrent neural network.

In this dataset, 75K samples (70K Training, 2.5K Validation, 2.5K Test) has been randomly selected from each category, processed with RDP line simplification with an epsilon parameter of 2.0. Each category will be stored in its own .npz file, for example, cat.npz.
- https://github.com/googlecreativelab/quickdraw-dataset/blob/master/README.md