Skip to content

Instantly share code, notes, and snippets.

@Mistobaan
Created February 4, 2026 04:35
Show Gist options
  • Select an option

  • Save Mistobaan/77ce74663cba2d32682cdacac1ffe735 to your computer and use it in GitHub Desktop.

Select an option

Save Mistobaan/77ce74663cba2d32682cdacac1ffe735 to your computer and use it in GitHub Desktop.
sharp-training.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4",
"authorship_tag": "ABX9TyO8nnTCSF1uQuolZRvf9YpG",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Mistobaan/77ce74663cba2d32682cdacac1ffe735/sharp-training.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ibLT2fAHeCp4"
},
"outputs": [],
"source": [
"!pip install condacolab"
]
},
{
"cell_type": "code",
"source": [
"!rm -fr /content/ml-sharp\n",
"!git clone https://github.com/Mistobaan/ml-sharp /content/ml-sharp"
],
"metadata": {
"id": "oGIs7BQheGJ6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import condacolab\n",
"condacolab.install()"
],
"metadata": {
"id": "H61mA2i1eYHl"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!conda create -n sharp python=3.13"
],
"metadata": {
"id": "CVlcayaqeOf4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import condacolab\n",
"condacolab.check()"
],
"metadata": {
"id": "sr5npHj-eTrs"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/ml-sharp"
],
"metadata": {
"id": "IlHSv9KWeLWJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%pip install -r requirements.txt"
],
"metadata": {
"id": "I5f-5QD7efid"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%env MPLBACKEND=notebook"
],
"metadata": {
"id": "bs0Fu3Dggazs"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!sharp --help"
],
"metadata": {
"id": "5GaiBY1jesXD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!wget https://www.hollywoodreporter.com/wp-content/uploads/2018/11/detective_pikachu-screengrab-trailer-3-h_2018.jpg -O /content/input.jpg"
],
"metadata": {
"id": "Y3txJmVcgQbu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!sharp predict -i /content/input.jpg -o /content/examples/01 --render"
],
"metadata": {
"id": "UE249fZpgy-9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from IPython.display import Video\n",
"\n",
"Video(\"/content/examples/01/input.mp4\", embed=True)"
],
"metadata": {
"id": "0qkFOfFWhr0_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/ml-sharp/\n",
"!git pull\n",
"!{sys.executable} -m pip install -e ."
],
"metadata": {
"id": "zZjfWEdF06MP"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from pathlib import Path\n",
"from sharp.utils.gsplat_notebook import show_ply, SplatViewerConfig\n",
"\n",
"show_ply(Path(\"/content/examples/01/input.ply\"), config=SplatViewerConfig(load_mode=\"inline\"))"
],
"metadata": {
"id": "UELj3CPGzWrd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import torch\n",
"import numpy as np\n",
"from plyfile import PlyData\n",
"import matplotlib.pyplot as plt\n",
"\n",
"def torch_ptp(input):\n",
" \"\"\"Calculates peak-to-peak (max - min) for a torch tensor.\"\"\"\n",
" return torch.max(input) - torch.min(input)\n",
"\n",
"def load_ply_as_torch(path):\n",
" \"\"\"\n",
" Loads a PLY point cloud (with x,y,z and optional rgb) into a PyTorch tensor.\n",
" \"\"\"\n",
" ply = PlyData.read(path)\n",
" vertex_data = ply['vertex'].data\n",
"\n",
" # Read xyz\n",
" xyz = np.vstack([vertex_data['x'], vertex_data['y'], vertex_data['z']]).T\n",
"\n",
" # Optional RGB if present\n",
" if {'red','green','blue'}.issubset(vertex_data.dtype.names):\n",
" rgb = np.vstack([\n",
" vertex_data['red'],\n",
" vertex_data['green'],\n",
" vertex_data['blue']\n",
" ]).T / 255.0\n",
" data = np.hstack([xyz, rgb])\n",
" else:\n",
" data = xyz\n",
"\n",
" return torch.from_numpy(data).float()\n",
"\n",
"def simple_project(points, image_size=(512,512)):\n",
" \"\"\"\n",
" Projects 3D points into 2D with a simple orthographic projection.\n",
" - points: Tensor (N,3)\n",
" - image_size: (H,W)\n",
" \"\"\"\n",
" # Center and scale to fit image\n",
" pts = points.clone()\n",
" pts -= pts.mean(0)\n",
" scale = max(torch_ptp(pts[:,0]), torch_ptp(pts[:,1]))\n",
" pts[:,:2] /= (scale + 1e-6)\n",
"\n",
" # Convert to pixel coords\n",
" H,W = image_size\n",
" u = ((pts[:,0] * 0.5 + 0.5) * (W-1)).long()\n",
" v = ((-pts[:,1] * 0.5 + 0.5) * (H-1)).long()\n",
"\n",
" return u.clamp(0,W-1), v.clamp(0,H-1)\n",
"\n",
"def render_pointcloud(points, colors=None, image_size=(512,512)):\n",
" \"\"\"\n",
" Renders a simple 2D image from a point cloud.\n",
" - points: Tensor (N,3)\n",
" - colors: Tensor (N,3) optional\n",
" \"\"\"\n",
" H,W = image_size\n",
" image = torch.zeros((H,W,3))\n",
"\n",
" u,v = simple_project(points, image_size)\n",
" for i in range(u.shape[0]):\n",
" if colors is not None:\n",
" image[v[i], u[i]] = colors[i]\n",
" else:\n",
" image[v[i], u[i]] = torch.tensor([1.0,1.0,1.0])\n",
"\n",
" return image"
],
"metadata": {
"id": "Su_I-zIFtpik"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Path to your PLY file\n",
"ply_path = \"/content/examples/01/input.ply\"\n",
"\n",
"data = load_ply_as_torch(ply_path)\n",
"\n",
"if data.shape[1] == 6:\n",
" points, colors = data[:,:3], data[:,3:]\n",
"else:\n",
" points, colors = data[:,:3], None\n",
"\n",
"img = render_pointcloud(points, colors)\n",
"\n",
"plt.figure(figsize=(6,6))\n",
"plt.imshow(img.numpy())\n",
"plt.axis('off')\n",
"plt.show()"
],
"metadata": {
"id": "q52VJwfQyeQP"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "rolFkhLc3qwB"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment