various additions for M1 GPU #299
No files matched your search
@@ -0,0 +1,50 @@
|
|||||||
|
name: torch-m1
|
||||||
|
channels:
|
||||||
|
- conda-forge
|
||||||
|
- apple
|
||||||
|
dependencies:
|
||||||
|
- python>=3.9
|
||||||
|
- pip
|
||||||
|
- cmake
|
||||||
|
- tensorflow-deps<=2.9.1
|
||||||
|
- tk
|
||||||
|
- libblas
|
||||||
|
- pip:
|
||||||
|
- tensorflow-macos==2.9.1
|
||||||
|
- tensorflow-metal
|
||||||
|
- tqdm>=4.64
|
||||||
|
- psutil>=5.8.0
|
||||||
|
- numpy>=1.18.0
|
||||||
|
- opencv-python>=4.5.5.0
|
||||||
|
- scikit-learn>=1.0.2
|
||||||
|
- imageio
|
||||||
|
- image
|
||||||
|
- timm
|
||||||
|
- PlL
|
||||||
|
- wrapt
|
||||||
|
- scikit-image
|
||||||
|
- moviepy
|
||||||
|
- Cython
|
||||||
|
- flatbuffers
|
||||||
|
- google-pasta
|
||||||
|
- h5py
|
||||||
|
- opt_einsum
|
||||||
|
- keras-nightly
|
||||||
|
- keras-preprocessing
|
||||||
|
- termcolor
|
||||||
|
- absl-py
|
||||||
|
- gast==0.4.0
|
||||||
|
- grpcio
|
||||||
|
- typing-extensions
|
||||||
|
- astunparse
|
||||||
|
- fastcluster>=1.2.4
|
||||||
|
- matplotlib>=3.5.1
|
||||||
|
- imageio>=2.9.0
|
||||||
|
- imageio-ffmpeg>=0.4.7
|
||||||
|
- ffmpy==0.2.3
|
||||||
|
- imageio==2.9.0
|
||||||
|
- torch
|
||||||
|
- torchvision==0.13.0
|
||||||
|
- insightface==0.2.1
|
||||||
|
- ipython==7.21.0
|
||||||
|
- CPython
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 138 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
conda init --all
|
||||||
|
conda create -n torch-gpu python=3.9
|
||||||
|
conda activate torch-gpu
|
||||||
|
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 -c pytorch
|
||||||
|
|
||||||
|
for i in `seq 1 6`; do
|
||||||
|
python3 m1_test.py
|
||||||
|
done
|
||||||
|
|
||||||
|
conda deactivate
|
||||||
|
|
||||||
|
exit 0
|
||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
#############################################################
|
||||||
|
# File: m1_test.py
|
||||||
|
# Created Date: Saturday July 9th 2022
|
||||||
|
# Author: Smiril
|
||||||
|
# Email: sonar@gmx.com
|
||||||
|
# Original: https://towardsdatascience.com/installing-pytorch-on-apple-m1-chip-with-gpu-acceleration-3351dc44d67c
|
||||||
|
#############################################################
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import math
|
||||||
|
# this ensures that the current MacOS version is at least 12.3+
|
||||||
|
print(torch.backends.mps.is_available())
|
||||||
|
# this ensures that the current current PyTorch installation was built with MPS activated.
|
||||||
|
print(torch.backends.mps.is_built())
|
||||||
|
dtype = torch.float
|
||||||
|
device = torch.device("mps")
|
||||||
|
|
||||||
|
# Create random input and output data
|
||||||
|
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
|
||||||
|
y = torch.sin(x)
|
||||||
|
|
||||||
|
# Randomly initialize weights
|
||||||
|
a = torch.randn((), device=device, dtype=dtype)
|
||||||
|
b = torch.randn((), device=device, dtype=dtype)
|
||||||
|
c = torch.randn((), device=device, dtype=dtype)
|
||||||
|
d = torch.randn((), device=device, dtype=dtype)
|
||||||
|
|
||||||
|
learning_rate = 1e-6
|
||||||
|
for t in range(2000):
|
||||||
|
# Forward pass: compute predicted y
|
||||||
|
y_pred = a + b * x + c * x ** 2 + d * x ** 3
|
||||||
|
|
||||||
|
# Compute and print loss
|
||||||
|
loss = (y_pred - y).pow(2).sum().item()
|
||||||
|
if t % 100 == 99:
|
||||||
|
print(t, loss)
|
||||||
|
|
||||||
|
# Backprop to compute gradients of a, b, c, d with respect to loss
|
||||||
|
grad_y_pred = 2.0 * (y_pred - y)
|
||||||
|
grad_a = grad_y_pred.sum()
|
||||||
|
grad_b = (grad_y_pred * x).sum()
|
||||||
|
grad_c = (grad_y_pred * x ** 2).sum()
|
||||||
|
grad_d = (grad_y_pred * x ** 3).sum()
|
||||||
|
|
||||||
|
# Update weights using gradient descent
|
||||||
|
a -= learning_rate * grad_a
|
||||||
|
b -= learning_rate * grad_b
|
||||||
|
c -= learning_rate * grad_c
|
||||||
|
d -= learning_rate * grad_d
|
||||||
|
|
||||||
|
|
||||||
|
print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
conda init --all
|
||||||
|
conda create --name tensorflow_m1 python==3.9
|
||||||
|
conda activate tensorflow_m1
|
||||||
|
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 -c pytorch
|
||||||
|
python3 -m venv ~/tensorflow-metal
|
||||||
|
source ~/tensorflow-metal/bin/activate
|
||||||
|
|
||||||
|
for i in `seq 1 6`; do
|
||||||
|
python3 m1_tf_test.py $(( ( RANDOM % 4096 ) + 4096 ))
|
||||||
|
done
|
||||||
|
|
||||||
|
conda deactivate
|
||||||
|
|
||||||
|
exit 0
|
||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
#############################################################
|
||||||
|
# File: m1_tf_test.py
|
||||||
|
# Created Date: Saturday July 9th 2022
|
||||||
|
# Author: Smiril
|
||||||
|
# Email: sonar@gmx.com
|
||||||
|
#############################################################
|
||||||
|
import sys
|
||||||
|
import tensorflow as tf
|
||||||
|
from tensorflow import keras
|
||||||
|
from tensorflow.keras.layers import Layer, Input, LSTM
|
||||||
|
from tensorflow.keras.models import *
|
||||||
|
tf.__version__
|
||||||
|
tf.config.list_physical_devices()
|
||||||
|
from random import randrange
|
||||||
|
|
||||||
|
class ComputeSum(Layer):
|
||||||
|
|
||||||
|
def __init__(self, input_dim):
|
||||||
|
super(ComputeSum, self).__init__()
|
||||||
|
# Create a trainable weight.
|
||||||
|
self.total = tf.Variable(initial_value=tf.zeros((input_dim,)),
|
||||||
|
trainable=True)
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
self.total.assign_add(tf.reduce_sum(inputs, axis=0))
|
||||||
|
return self.total
|
||||||
|
|
||||||
|
def call(self, data, depth = 0):
|
||||||
|
n = len(data)
|
||||||
|
return n
|
||||||
|
|
||||||
|
def ComputeSumModel(input_shape):
|
||||||
|
inputs = Input(shape = input_shape)
|
||||||
|
outputs = ComputeSum(input_shape[0])(inputs)
|
||||||
|
model = tf.keras.Model(inputs = inputs, outputs = outputs)
|
||||||
|
|
||||||
|
return model
|
||||||
|
|
||||||
|
def xatoi(Str):
|
||||||
|
|
||||||
|
sign, base, i = 1, 0, 0
|
||||||
|
|
||||||
|
# If whitespaces then ignore.
|
||||||
|
while (Str[i] == ' '):
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Sign of number
|
||||||
|
if (Str[i] == '-' or Str[i] == '+'):
|
||||||
|
sign = 1 - 2 * (Str[i] == '-')
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Checking for valid input
|
||||||
|
while (i < len(Str) and
|
||||||
|
Str[i] >= '0' and Str[i] <= '9'):
|
||||||
|
|
||||||
|
# Handling overflow test case
|
||||||
|
if (base > (sys.maxsize // 10) or
|
||||||
|
(base == (sys.maxsize // 10) and
|
||||||
|
(Str[i] - '0') > 7)):
|
||||||
|
if (sign == 1):
|
||||||
|
return sys.maxsize
|
||||||
|
else:
|
||||||
|
return -(sys.maxsize)
|
||||||
|
|
||||||
|
base = 10 * base + (ord(Str[i]) - ord('0'))
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return base * sign
|
||||||
|
|
||||||
|
inputs = tf.keras.Input(shape=(xatoi(sys.argv[1]),), name="digits")
|
||||||
|
|
||||||
|
mnist = tf.keras.datasets.mnist
|
||||||
|
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||||
|
x_train, x_test = x_train / randrange(255+1), x_test / randrange(255+1)
|
||||||
|
model = tf.keras.models.Sequential([
|
||||||
|
tf.keras.layers.Flatten(input_shape=(28, 28)),
|
||||||
|
tf.keras.layers.Dense(128,activation='selu',name='layer1'),
|
||||||
|
tf.keras.layers.Dropout(0.2),
|
||||||
|
tf.keras.layers.Dense(64,activation='relu',name='layer2'),
|
||||||
|
tf.keras.layers.Dropout(0.2),
|
||||||
|
tf.keras.layers.Dense(32,activation='elu',name='layer3'),
|
||||||
|
tf.keras.layers.Dropout(0.2),
|
||||||
|
tf.keras.layers.Dense(16,activation='tanh',name='layer4'),
|
||||||
|
tf.keras.layers.Dropout(0.2),
|
||||||
|
tf.keras.layers.Dense(8,activation='sigmoid',name='layer5'),
|
||||||
|
tf.keras.layers.Dropout(0.2)
|
||||||
|
])
|
||||||
|
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
|
||||||
|
model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])
|
||||||
|
these = model.fit(x_train, y_train, epochs=10).history
|
||||||
|
that = ComputeSum(len(these))
|
||||||
|
those = that(these)
|
||||||
|
print(those)
|
||||||
|
model = ComputeSumModel((tf.shape(these)[1],))
|
||||||
|
print(model(these))
|
||||||
|
model = tf.keras.Model(inputs=inputs, outputs=outputs)
|
||||||
|
model.build()
|
||||||
|
model.save(these)
|
||||||
|
model.summary()
|
||||||
+7
-1
@@ -48,7 +48,13 @@ class fsModel(BaseModel):
|
|||||||
torch.backends.cudnn.benchmark = True
|
torch.backends.cudnn.benchmark = True
|
||||||
self.isTrain = opt.isTrain
|
self.isTrain = opt.isTrain
|
||||||
|
|
||||||
device = torch.device("cuda:0")
|
if not torch.backends.mps.is_available():
|
||||||
|
if not torch.backends.mps.is_built():
|
||||||
|
device = torch.device("cuda:0")
|
||||||
|
else:
|
||||||
|
print("ERROR")
|
||||||
|
else:
|
||||||
|
device = torch.device("mps")
|
||||||
|
|
||||||
if opt.crop_size == 224:
|
if opt.crop_size == 224:
|
||||||
from .fs_networks import Generator_Adain_Upsample, Discriminator
|
from .fs_networks import Generator_Adain_Upsample, Discriminator
|
||||||
|
|||||||
+10
-2
@@ -80,8 +80,16 @@ class BaseOptions():
|
|||||||
self.opt.gpu_ids.append(id)
|
self.opt.gpu_ids.append(id)
|
||||||
|
|
||||||
# set gpu ids
|
# set gpu ids
|
||||||
if len(self.opt.gpu_ids) > 0:
|
if not torch.backends.mps.is_available():
|
||||||
torch.cuda.set_device(self.opt.gpu_ids[0])
|
if not torch.backends.mps.is_built():
|
||||||
|
if len(self.opt.gpu_ids) > 0:
|
||||||
|
torch.cuda.set_device(self.opt.gpu_ids[0])
|
||||||
|
else:
|
||||||
|
print("ERROR")
|
||||||
|
else:
|
||||||
|
print("ERROR")
|
||||||
|
else:
|
||||||
|
torch.device("mps")
|
||||||
|
|
||||||
args = vars(self.opt)
|
args = vars(self.opt)
|
||||||
|
|
||||||
|
|||||||
+25
-10
@@ -49,16 +49,31 @@ if __name__ == '__main__':
|
|||||||
img_att = img_b.view(-1, img_b.shape[0], img_b.shape[1], img_b.shape[2])
|
img_att = img_b.view(-1, img_b.shape[0], img_b.shape[1], img_b.shape[2])
|
||||||
|
|
||||||
# convert numpy to tensor
|
# convert numpy to tensor
|
||||||
img_id = img_id.cuda()
|
if not torch.backends.mps.is_available():
|
||||||
img_att = img_att.cuda()
|
if not torch.backends.mps.is_built():
|
||||||
|
img_id = img_id.cuda()
|
||||||
|
img_att = img_att.cuda()
|
||||||
|
else:
|
||||||
|
print("ERROR")
|
||||||
|
else:
|
||||||
|
img_id = img_id.has_mps()
|
||||||
|
img_att = img_att.has_mps()
|
||||||
#create latent id
|
#create latent id
|
||||||
img_id_downsample = F.interpolate(img_id, size=(112,112))
|
if not torch.backends.mps.is_available():
|
||||||
latend_id = model.netArc(img_id_downsample)
|
if not torch.backends.mps.is_built():
|
||||||
latend_id = latend_id.detach().to('cpu')
|
img_id_downsample = F.interpolate(img_id, size=(112,112))
|
||||||
latend_id = latend_id/np.linalg.norm(latend_id,axis=1,keepdims=True)
|
latend_id = model.netArc(img_id_downsample)
|
||||||
latend_id = latend_id.to('cuda')
|
latend_id = latend_id.detach().to('cpu')
|
||||||
|
latend_id = latend_id/np.linalg.norm(latend_id,axis=1,keepdims=True)
|
||||||
|
latend_id = latend_id.to('cuda')
|
||||||
|
else:
|
||||||
|
print("ERROR")
|
||||||
|
else:
|
||||||
|
img_id_downsample = F.interpolate(img_id, size=(112,112))
|
||||||
|
latend_id = model.netArc(img_id_downsample)
|
||||||
|
latend_id = latend_id.detach().to('cpu')
|
||||||
|
latend_id = latend_id/np.linalg.norm(latend_id,axis=1,keepdims=True)
|
||||||
|
latend_id = latend_id.to('has_mps')
|
||||||
|
|
||||||
############## Forward Pass ######################
|
############## Forward Pass ######################
|
||||||
img_fake = model(img_id, img_att, latend_id, latend_id, True)
|
img_fake = model(img_id, img_att, latend_id, latend_id, True)
|
||||||
@@ -83,4 +98,4 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
output = output*255
|
output = output*255
|
||||||
|
|
||||||
cv2.imwrite(opt.output_path + 'result.jpg',output)
|
cv2.imwrite(opt.output_path + 'result.jpg',output)
|
||||||
Reference in new issue
Block a user