All files / web/src/app/vision-training/train/components/wizard/cards ConfigCard.tsx

0% Statements 0/329
0% Branches 0/1
0% Functions 0/1
0% Lines 0/329

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
'use client'

import { css } from '../../../../../../../styled-system/css'
import type { TrainingConfig, ModelType } from '../types'

interface ConfigCardProps {
  config: TrainingConfig
  setConfig: (config: TrainingConfig | ((prev: TrainingConfig) => TrainingConfig)) => void
  isGpu: boolean
  onStartTraining: () => void
  canStart: boolean
  /** Total number of training images */
  totalImages: number
  /** Which model type is being trained */
  modelType: ModelType | null
}

interface Preset {
  epochs: number
  batchSize: number
  label: string
  desc: string
}

export function ConfigCard({
  config,
  setConfig,
  isGpu,
  onStartTraining,
  canStart,
  totalImages,
  modelType,
}: ConfigCardProps) {
  // Recommend batch size based on dataset size
  // Goal: at least 10-20 batches per epoch for meaningful gradient updates
  const getRecommendedBatchSize = (images: number, hasGpu: boolean): number => {
    if (images < 200) return 8
    if (images < 500) return 16
    if (images < 2000) return 32
    return hasGpu ? 64 : 32
  }

  const recommendedBatchSize = getRecommendedBatchSize(totalImages, isGpu)
  const batchesPerEpoch = Math.floor(totalImages / config.batchSize)

  // Hardware and dataset-aware presets
  const presets: Record<string, Preset> = isGpu
    ? {
        quick: {
          epochs: 10,
          batchSize: recommendedBatchSize,
          label: '⚡ Quick',
          desc: '~2 min',
        },
        balanced: {
          epochs: 50,
          batchSize: recommendedBatchSize,
          label: '⚖️ Balanced',
          desc: '~10 min',
        },
        best: {
          epochs: 100,
          batchSize: recommendedBatchSize,
          label: '✨ Best',
          desc: '~20 min',
        },
      }
    : {
        quick: {
          epochs: 5,
          batchSize: recommendedBatchSize,
          label: '⚡ Quick',
          desc: '~5 min',
        },
        balanced: {
          epochs: 25,
          batchSize: recommendedBatchSize,
          label: '⚖️ Balanced',
          desc: '~15 min',
        },
        best: {
          epochs: 50,
          batchSize: recommendedBatchSize,
          label: '✨ Best',
          desc: '~30 min',
        },
      }

  const applyPreset = (preset: Preset) => {
    setConfig((prev) => ({
      ...prev,
      epochs: preset.epochs,
      batchSize: preset.batchSize,
    }))
  }

  const isPresetActive = (preset: Preset) =>
    config.epochs === preset.epochs && config.batchSize === preset.batchSize

  return (
    <div>
      {/* Presets */}
      <div className={css({ mb: 4 })}>
        <div className={css({ fontSize: 'xs', color: 'gray.500', mb: 2 })}>Presets</div>
        <div className={css({ display: 'flex', gap: 2 })}>
          {Object.entries(presets).map(([key, preset]) => (
            <button
              key={key}
              type="button"
              onClick={() => applyPreset(preset)}
              className={css({
                flex: 1,
                py: 2,
                px: 2,
                borderRadius: 'lg',
                border: '2px solid',
                borderColor: isPresetActive(preset) ? 'blue.500' : 'gray.700',
                bg: isPresetActive(preset) ? 'blue.900' : 'gray.800',
                color: isPresetActive(preset) ? 'blue.300' : 'gray.300',
                cursor: 'pointer',
                transition: 'all 0.2s',
                _hover: { borderColor: 'blue.400' },
              })}
            >
              <div className={css({ fontSize: 'sm', fontWeight: 'medium' })}>{preset.label}</div>
              <div className={css({ fontSize: 'xs', color: 'gray.500' })}>{preset.desc}</div>
            </button>
          ))}
        </div>
      </div>

      {/* Epochs slider */}
      <div className={css({ mb: 4 })}>
        <div
          className={css({
            display: 'flex',
            justifyContent: 'space-between',
            mb: 1,
          })}
        >
          <span className={css({ fontSize: 'xs', color: 'gray.500' })}>Training Rounds</span>
          <span
            className={css({
              fontSize: 'sm',
              fontWeight: 'medium',
              color: 'gray.200',
            })}
          >
            {config.epochs}
          </span>
        </div>
        <input
          type="range"
          min={5}
          max={isGpu ? 150 : 75}
          value={config.epochs}
          onChange={(e) =>
            setConfig((prev) => ({
              ...prev,
              epochs: parseInt(e.target.value, 10),
            }))
          }
          className={css({
            width: '100%',
            height: '6px',
            borderRadius: 'full',
            bg: 'gray.700',
            appearance: 'none',
            cursor: 'pointer',
            '&::-webkit-slider-thumb': {
              appearance: 'none',
              width: '16px',
              height: '16px',
              borderRadius: 'full',
              bg: 'blue.500',
              cursor: 'pointer',
            },
          })}
        />
      </div>

      {/* Batch size */}
      <div className={css({ mb: 4 })}>
        <div
          className={css({
            display: 'flex',
            justifyContent: 'space-between',
            mb: 1,
          })}
        >
          <span className={css({ fontSize: 'xs', color: 'gray.500' })}>Batch Size</span>
          <span
            className={css({
              fontSize: 'sm',
              fontWeight: 'medium',
              color: 'gray.200',
            })}
          >
            {config.batchSize}
            {config.batchSize === recommendedBatchSize && (
              <span className={css({ color: 'green.400', ml: 1 })}>(recommended)</span>
            )}
          </span>
        </div>
        <div className={css({ display: 'flex', gap: 2 })}>
          {[8, 16, 32, 64].map((size) => {
            const isRecommended = size === recommendedBatchSize
            return (
              <button
                key={size}
                type="button"
                onClick={() => setConfig((prev) => ({ ...prev, batchSize: size }))}
                className={css({
                  flex: 1,
                  py: 1.5,
                  borderRadius: 'md',
                  border: '1px solid',
                  borderColor:
                    config.batchSize === size
                      ? 'blue.500'
                      : isRecommended
                        ? 'green.700'
                        : 'gray.700',
                  bg: config.batchSize === size ? 'blue.900' : 'transparent',
                  color: config.batchSize === size ? 'blue.300' : 'gray.400',
                  fontSize: 'sm',
                  cursor: 'pointer',
                  _hover: { borderColor: 'blue.400' },
                })}
              >
                {size}
              </button>
            )
          })}
        </div>
        {/* Batch size guidance */}
        <div className={css({ fontSize: 'xs', color: 'gray.500', mt: 2 })}>
          {totalImages === 0 ? (
            <span>Smaller = better for small datasets, larger = faster training</span>
          ) : batchesPerEpoch < 10 ? (
            <span className={css({ color: 'yellow.400' })}>
              ⚠️ Only {batchesPerEpoch} batches/epoch — consider smaller batch size
            </span>
          ) : (
            <span>
              {batchesPerEpoch} batches/epoch • Smaller = better for small datasets, larger = faster
              training
            </span>
          )}
        </div>
      </div>

      {/* Color augmentation toggle - only for boundary detector */}
      {modelType === 'boundary-detector' && (
        <div className={css({ mb: 4 })}>
          <div
            className={css({
              display: 'flex',
              justifyContent: 'space-between',
              alignItems: 'center',
            })}
          >
            <div>
              <span className={css({ fontSize: 'sm', color: 'gray.300' })}>Color Augmentation</span>
              <div className={css({ fontSize: 'xs', color: 'gray.500' })}>
                Random brightness/contrast/saturation variations
              </div>
            </div>
            <button
              type="button"
              onClick={() =>
                setConfig((prev) => ({
                  ...prev,
                  colorAugmentation: !prev.colorAugmentation,
                }))
              }
              className={css({
                width: '48px',
                height: '26px',
                borderRadius: 'full',
                border: 'none',
                cursor: 'pointer',
                position: 'relative',
                transition: 'background 0.2s',
                bg: config.colorAugmentation ? 'green.600' : 'gray.700',
              })}
              data-setting="color-augmentation"
            >
              <div
                className={css({
                  position: 'absolute',
                  top: '3px',
                  width: '20px',
                  height: '20px',
                  borderRadius: 'full',
                  bg: 'white',
                  transition: 'left 0.2s',
                  left: config.colorAugmentation ? '25px' : '3px',
                })}
              />
            </button>
          </div>
        </div>
      )}

      {/* Start button */}
      <button
        type="button"
        onClick={onStartTraining}
        disabled={!canStart}
        className={css({
          width: '100%',
          py: 3,
          bg: canStart ? 'green.600' : 'gray.700',
          color: canStart ? 'white' : 'gray.500',
          borderRadius: 'lg',
          border: 'none',
          cursor: canStart ? 'pointer' : 'not-allowed',
          fontWeight: 'bold',
          fontSize: 'md',
          transition: 'all 0.2s',
          _hover: canStart ? { bg: 'green.500' } : {},
        })}
      >
        {canStart ? 'Start Training →' : 'Complete previous steps first'}
      </button>
    </div>
  )
}