diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..097f77ef --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM ubuntu:20.04 + +RUN apt-get install build-essential git python3-dev python3-pip libopenexr-dev libxi-dev \ +libglfw3-dev libglew-dev libomp-dev libxinerama-dev libxcursor-dev \ No newline at end of file diff --git a/MyLog/run1_observation.md b/MyLog/run1_observation.md new file mode 100644 index 00000000..2ee4caf0 --- /dev/null +++ b/MyLog/run1_observation.md @@ -0,0 +1,19 @@ +# Run1 Observation + +## Data +`image_4/` has higher resolution compared with `images_8/` +- `images` (Original data) has higher resolution compared with `images_4/` +- All folders contain images from 00-19. (Though their name is different) +Train data: [ 1 2 3 4 5 6 7 9 10 11 12 13 14 15 17 18 19] +Test and Val data: [0 8 16] + +## Training - `train()` (from line 575) +- `nerf` model is created and define in `create_nerf(args)` function. + +- main part are on the `Core optimization loop`. + +## Nerf: `create_nerf(args)` (from line 378): initialize the NeRE's MLP model +- `init_nerf_model` (from `run_nerf_helpers.py`) + + + diff --git a/dockerfiles_old/Dockerfile b/dockerfiles_old/Dockerfile new file mode 100644 index 00000000..a75ba546 --- /dev/null +++ b/dockerfiles_old/Dockerfile @@ -0,0 +1,54 @@ +# Successfully built dockerfile to use Nsight Systems on Ubuntu 20.04 with pyenv and pyenv-virtualenv +FROM nvidia/cuda:12.3.2-cudnn9-devel-ubuntu20.04 + +RUN apt-get update && \ + apt-get install -y wget +# RUN git --version + +# ## Install Nsight Systems +RUN wget https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2024_4/NsightSystems-linux-cli-public-2024.4.1.61-3431596.deb + +# # Set the timezone +RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime + +RUN apt-get install -y ./NsightSystems-linux-cli-public-2024.4.1.61-3431596.deb + +RUN rm ./NsightSystems-linux-cli-public-2024.4.1.61-3431596.deb && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +RUN sysctl -w kernel.perf_event_paranoid=1 + +# ## Install basic tools (list of necessary tools are taken from https://qiita.com/nyakiri_0726/items/a33f404b5e1be9352b85) +# `apt-get update` is necessary before `apt-get install` to avoid error +# RUN apt-get update && apt-get install -y make build-essential git vim zlib1g-dev +RUN apt-get update && apt-get install -y build-essential git vim libssl-dev zlib1g-dev libbz2-dev libreadline-dev \ + libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev \ + liblzma-dev python-openssl git vim less + +# # Environment variables +ENV HOME /root +ENV PYENV_ROOT $HOME/.pyenv +ENV PATH $PYENV_ROOT/bin:$PATH +ARG PYTHON_VERSION=3.7.17 + +RUN git clone https://github.com/pyenv/pyenv.git ~/.pyenv + +RUN echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc && \ + echo 'eval "$(pyenv init --path)"' >> ~/.bashrc && \ + echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \ + echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc && \ + echo 'export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python' >> ~/.bashrc + +# it may takes time in `patching file setup.py` +RUN pyenv install 3.7.17 + +# Install pyenv-virtualenv +# NOTE: it should not be `$(PYENV_ROOT)/plugins/pyenv-virtualenv` but `$PYENV_ROOT/plugins/pyenv-virtualenv` +RUN git clone https://github.com/pyenv/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv +RUN echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc +# RUN source ~/.bashrc +RUN pyenv virtualenv 3.7.17 nerf_env + + +CMD [ "/bin/bash" ] diff --git a/dockerfiles_old/Dockerfile copy b/dockerfiles_old/Dockerfile copy new file mode 100644 index 00000000..d539b61f --- /dev/null +++ b/dockerfiles_old/Dockerfile copy @@ -0,0 +1,49 @@ +FROM nvidia/cuda:12.3.2-cudnn9-devel-ubuntu20.04 + +RUN apt-get update && \ + apt-get install -y wget + +## Install Nsight Systems +RUN wget https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2024_4/NsightSystems-linux-cli-public-2024.4.1.61-3431596.deb + +# Set the timezone +RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime + +RUN apt-get install -y ./NsightSystems-linux-cli-public-2024.4.1.61-3431596.deb + +RUN rm ./NsightSystems-linux-cli-public-2024.4.1.61-3431596.deb && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +RUN sysctl -w kernel.perf_event_paranoid=1 + +## Install basic tools +RUN apt-get install -y make build-essential +# RUN curl https://pyenv.run | bash +# RUN pyenv install 3.7.17 + +RUN echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc && \ + echo 'eval "$(pyenv init --path)"' >> ~/.bashrc && \ + echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \ + echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc && \ + echo 'export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python' >> ~/.bashrc + +# RUN /root/.pyenv/bin/pyenv install 3.7.17 && \ +# /root/.pyenv/bin/pyenv global 3.7.17 + +RUN wget https://www.python.org/ftp/python/3.7.17/Python-3.7.17.tgz && \ + tar -xzf Python-3.7.17.tgz + +RUN cd Python-3.7.17 && \ + ./configure --enable-optimizations && \ + make -j$(nproc) && \ + make altinstall + +RUN rm -rf Python-3.7.17 Python-3.7.17.tgz && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +RUN update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.7 1 && \ + update-alternatives --install /usr/bin/pip3 pip3 /usr/local/bin/pip3.7 1 + +CMD [ "/bin/bash" ] diff --git a/dockerfiles_old/Dockerfile_torchprofiler b/dockerfiles_old/Dockerfile_torchprofiler new file mode 100644 index 00000000..a98a0466 --- /dev/null +++ b/dockerfiles_old/Dockerfile_torchprofiler @@ -0,0 +1,41 @@ +FROM ubuntu:20.04 + +ENV HOME /root +ENV PYENV_ROOT $HOME/.pyenv +ENV PATH $PYENV_ROOT/bin:$PATH + +# # Set the timezone +RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime + +RUN apt-get update && apt-get install -y wget build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \ + libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev \ + liblzma-dev python-openssl git vim less + + +RUN git clone https://github.com/pyenv/pyenv.git ~/.pyenv + +RUN echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc && \ + echo 'eval "$(pyenv init --path)"' >> ~/.bashrc && \ + echo 'eval "$(pyenv init -)"' >> ~/.bashrc && \ + echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc && \ + echo 'export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python' >> ~/.bashrc + +# it may takes time in `patching file setup.py` +RUN pyenv install 3.7.17 + +# Install pyenv-virtualenv +# NOTE: it should not be `$(PYENV_ROOT)/plugins/pyenv-virtualenv` but `$PYENV_ROOT/plugins/pyenv-virtualenv` +RUN git clone https://github.com/pyenv/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv +RUN echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc +# RUN source ~/.bashrc +RUN pyenv virtualenv 3.7.17 nerf_env + +# RUN cd $PYENV_ROOT && \ +# src/configure && \ +# make -C src + +RUN . $HOME/.bashrc +RUN pyenv activate nerf_env +RUN pip install -r requirements.txt +RUN pip install torchprofiler + \ No newline at end of file diff --git a/load_llff.py b/load_llff.py index 98b79163..e2e94790 100644 --- a/load_llff.py +++ b/load_llff.py @@ -107,7 +107,7 @@ def _load_data(basedir, factor=None, width=None, height=None, load_imgs=True): def imread(f): if f.endswith('png'): - return imageio.imread(f, ignoregamma=True) + return imageio.imread(f, apply_gamma=False) else: return imageio.imread(f) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..aff21fbe --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +tensorflow-gpu==1.15 +numpy +matplotlib +imageio +imageio-ffmpeg +configargparse +imagemagick diff --git a/run_nerf.py b/run_nerf.py index d75f051a..6f2979c1 100644 --- a/run_nerf.py +++ b/run_nerf.py @@ -742,8 +742,7 @@ def train(): np.random.shuffle(rays_rgb) print('done') i_batch = 0 - - N_iters = 1000000 + N_iters = 3 print('Begin') print('TRAIN views are', i_train) print('TEST views are', i_test) @@ -802,16 +801,24 @@ def train(): batch_rays = tf.stack([rays_o, rays_d], 0) target_s = tf.gather_nd(target, select_inds) + time1 = time.time() + ray_time = time1 - time0 + ##### Core optimization loop ##### with tf.GradientTape() as tape: # Make predictions for color, disparity, accumulated opacity. + # NOTE: これがメインの処理: 3 -> 4に相当 + time_before_render = time.time() rgb, disp, acc, extras = render( H, W, focal, chunk=args.chunk, rays=batch_rays, verbose=i < 10, retraw=True, **render_kwargs_train) + time_after_render = time.time() + time_render = time_after_render - time_before_render # Compute MSE loss between predicted and true RGB. + time_before_loss = time.time() img_loss = img2mse(rgb, target_s) trans = extras['raw'][..., -1] loss = img_loss @@ -822,9 +829,15 @@ def train(): img_loss0 = img2mse(extras['rgb0'], target_s) loss += img_loss0 psnr0 = mse2psnr(img_loss0) + + time_after_loss = time.time() + time_calc_loss = time_after_loss - time_before_loss + time2 = time.time() gradients = tape.gradient(loss, grad_vars) optimizer.apply_gradients(zip(gradients, grad_vars)) + time3 = time.time() + time_backprop = time3 - time2 dt = time.time()-time0 @@ -874,7 +887,9 @@ def save_weights(net, prefix, i): if i % args.i_print == 0 or i < 10: print(expname, i, psnr.numpy(), loss.numpy(), global_step.numpy()) - print('iter time {:.05f}'.format(dt)) + print('iter time {:.05f} (ray calc: {:.05f} ({:.02f}%), rendering: {:.05f} ({:.02f}%), loss: {:.05f} ({:.02f}%), backprop: {:.05f} ({:.02f}%))'.format(dt, ray_time, ray_time/dt*100, time_render, time_render/dt*100, time_calc_loss, time_calc_loss/dt*100, time_backprop, time_backprop/dt*100)) + + with tf.contrib.summary.record_summaries_every_n_global_steps(args.i_print): tf.contrib.summary.scalar('loss', loss) tf.contrib.summary.scalar('psnr', psnr) diff --git a/sample.cu b/sample.cu new file mode 100644 index 00000000..c656f975 --- /dev/null +++ b/sample.cu @@ -0,0 +1,18 @@ +#include "stdio.h" +__global__ void add(int a, int b, int *c) +{ + *c = a + b; +} +int main() +{ + int a, b, c; + int *dev_c; + a = 3; + b = 4; + cudaMalloc((void **)&dev_c, sizeof(int)); + add<<<1, 1>>>(a, b, dev_c); + cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost); + printf("%d + %d is %d\n", a, b, c); + cudaFree(dev_c); + return 0; +} \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 00000000..f7e26287 --- /dev/null +++ b/test.py @@ -0,0 +1,29 @@ +import torch +import torch.nn as nn + +# Define a simple neural network +class SimpleNet(nn.Module): + def __init__(self): + super(SimpleNet, self).__init__() + self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1) + self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1) + self.fc1 = nn.Linear(32 * 8 * 8, 128) + self.fc2 = nn.Linear(128, 10) + + def forward(self, x): + x = torch.relu(self.conv1(x)) + x = torch.relu(self.conv2(x)) + x = torch.flatten(x, 1) + x = torch.relu(self.fc1(x)) + x = self.fc2(x) + return x + +# Create a model instance +model = SimpleNet() + +# Create a dummy input tensor +dummy_input = torch.randn(1, 3, 8, 8) + +# Profile the FLOPS +output = model(dummy_input) +print("Finished!") diff --git a/test2.py b/test2.py new file mode 100644 index 00000000..7f0fdedc --- /dev/null +++ b/test2.py @@ -0,0 +1,34 @@ +import torch +import torch.nn as nn +import torchprofile + +# Define a simple neural network +class SimpleNet(nn.Module): + def __init__(self): + super(SimpleNet, self).__init__() + self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1) + self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1) + self.fc1 = nn.Linear(32 * 8 * 8, 128) + self.fc2 = nn.Linear(128, 10) + + def forward(self, x): + x = torch.relu(self.conv1(x)) + x = torch.relu(self.conv2(x)) + x = torch.flatten(x, 1) + x = torch.relu(self.fc1(x)) + x = self.fc2(x) + return x + +# Create a model instance +model = SimpleNet() + +# Create a dummy input tensor +dummy_input = torch.randn(1, 3, 8, 8) + +# Profile the FLOPS +with torchprofile.profile(model, dummy_input) as profiler: + output = model(dummy_input) + +# Print the profiling results +print(profiler) + diff --git a/test3.py b/test3.py new file mode 100644 index 00000000..0ecd7ed4 --- /dev/null +++ b/test3.py @@ -0,0 +1,33 @@ +import torch +import torch.nn as nn +from torchprofile import profile # Import the profile function + +# Define a simple neural network +class SimpleNet(nn.Module): + def __init__(self): + super(SimpleNet, self).__init__() + self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1) + self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1) + self.fc1 = nn.Linear(32 * 8 * 8, 128) + self.fc2 = nn.Linear(128, 10) + + def forward(self, x): + x = torch.relu(self.conv1(x)) + x = torch.relu(self.conv2(x)) + x = torch.flatten(x, 1) + x = torch.relu(self.fc1(x)) + x = self.fc2(x) + return x + +# Create a model instance +model = SimpleNet() + +# Create a dummy input tensor +dummy_input = torch.randn(1, 3, 8, 8) + +# Profile the FLOPS +with profile(model, dummy_input) as profiler: + output = model(dummy_input) + +# Print the profiling results +print(profiler) diff --git a/test4.py b/test4.py new file mode 100644 index 00000000..a7acbdc7 --- /dev/null +++ b/test4.py @@ -0,0 +1,13 @@ +import tensorflow as tf + +# Initialize profiler +tf.profiler.experimental.start(logdir='path/to/logs') + +# Define and run your TensorFlow operations +a = tf.constant(1) +b = tf.constant(2) +c = a + b +print(c) + +# Stop profiler +tf.profiler.experimental.stop()