import os import time import numpy as np from extra.efficientnet import EfficientNet from tinygrad.tensor import Tensor if __name__ == "__main__": Tensor.default_gpu = os.getenv("GPU") is not None model = EfficientNet(int(os.getenv("NUM", "0"))) BS = 16 img = np.zeros((BS, 3, 224, 224), dtype=np.float32) for i in range(32): print("running batch %d" % i) st = time.time() out = model.forward(Tensor(img)) et = time.time() print("forward %.2f s" % (et - st)) Y = [0] * BS y = np.zeros((BS, 1000), np.float32) y[range(y.shape[0]), Y] = -1000.0 y = Tensor(y) loss = out.logsoftmax().mul(y).mean() st = time.time() loss.backward() et = time.time()
X = db[b'data'].reshape((-1, 3, 32, 32)) Y = np.array(db[b'labels']) return X, Y if __name__ == "__main__": X_train, Y_train = load_cifar() classes = 10 Tensor.default_gpu = os.getenv("GPU") is not None TINY = os.getenv("TINY") is not None TRANSFER = os.getenv("TRANSFER") is not None if TINY: model = TinyConvNet(classes) elif TRANSFER: model = EfficientNet(int(os.getenv("NUM", "0")), classes, has_se=True) model.load_weights_from_torch() else: model = EfficientNet(int(os.getenv("NUM", "0")), classes, has_se=False) parameters = get_parameters(model) print("parameters", len(parameters)) optimizer = optim.Adam(parameters, lr=0.001) #BS, steps = 16, 32 BS, steps = 64 if TINY else 16, 2048 for i in (t := trange(steps)): samp = np.random.randint(0, X_train.shape[0], size=(BS)) img = X_train[samp].astype(np.float32)
out = model.forward(Tensor(img).gpu()).cpu() else: out = model.forward(Tensor(img)) # if you want to look at the outputs """ import matplotlib.pyplot as plt plt.plot(out.data[0]) plt.show() """ return out, retimg if __name__ == "__main__": # instantiate my net model = EfficientNet(int(os.getenv("NUM", "0"))) model.load_weights_from_torch() if GPU: [x.gpu_() for x in get_parameters(model)] # category labels import ast lbls = fetch( "https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5/imagenet1000_clsidx_to_labels.txt" ) lbls = ast.literal_eval(lbls.decode('utf-8')) # load image and preprocess from PIL import Image url = sys.argv[1] if url == 'webcam':
import os import time import numpy as np from extra.efficientnet import EfficientNet from tinygrad.tensor import Tensor if __name__ == "__main__": Tensor.default_gpu = os.getenv("GPU") is not None model = EfficientNet() BS = 4 img = np.zeros((BS, 3, 224, 224), dtype=np.float32) st = time.time() out = model.forward(Tensor(img)) et = time.time() print("forward %.2f s" % (et - st)) Y = [0] * BS y = np.zeros((BS, 1000), np.float32) y[range(y.shape[0]), Y] = -1000.0 y = Tensor(y) loss = out.logsoftmax().mul(y).mean() st = time.time() loss.backward() et = time.time() print("backward %.2f s" % (et - st))